Staging: rtl8187se: remove kernel version compatibility wrappers
[safe/jmp/linux-2.6] / drivers / staging / rtl8187se / ieee80211 / ieee80211_softmac.c
1 /* IEEE 802.11 SoftMAC layer
2  * Copyright (c) 2005 Andrea Merello <andreamrl@tiscali.it>
3  *
4  * Mostly extracted from the rtl8180-sa2400 driver for the
5  * in-kernel generic ieee802.11 stack.
6  *
7  * Few lines might be stolen from other part of the ieee80211
8  * stack. Copyright who own it's copyright
9  *
10  * WPA code stolen from the ipw2200 driver.
11  * Copyright who own it's copyright.
12  *
13  * released under the GPL
14  */
15
16
17 #include "ieee80211.h"
18
19 #include <linux/random.h>
20 #include <linux/delay.h>
21 #include <linux/version.h>
22 #include <asm/uaccess.h>
23
24 #ifdef ENABLE_DOT11D
25 #include "dot11d.h"
26 #endif
27 u8 rsn_authen_cipher_suite[16][4] = {
28         {0x00,0x0F,0xAC,0x00}, //Use group key, //Reserved
29         {0x00,0x0F,0xAC,0x01}, //WEP-40         //RSNA default
30         {0x00,0x0F,0xAC,0x02}, //TKIP           //NONE          //{used just as default}
31         {0x00,0x0F,0xAC,0x03}, //WRAP-historical
32         {0x00,0x0F,0xAC,0x04}, //CCMP
33         {0x00,0x0F,0xAC,0x05}, //WEP-104
34 };
35
36 short ieee80211_is_54g(struct ieee80211_network net)
37 {
38         return ((net.rates_ex_len > 0) || (net.rates_len > 4));
39 }
40
41 short ieee80211_is_shortslot(struct ieee80211_network net)
42 {
43         return (net.capability & WLAN_CAPABILITY_SHORT_SLOT);
44 }
45
46 /* returns the total length needed for pleacing the RATE MFIE
47  * tag and the EXTENDED RATE MFIE tag if needed.
48  * It encludes two bytes per tag for the tag itself and its len
49  */
50 unsigned int ieee80211_MFIE_rate_len(struct ieee80211_device *ieee)
51 {
52         unsigned int rate_len = 0;
53
54         if (ieee->modulation & IEEE80211_CCK_MODULATION)
55                 rate_len = IEEE80211_CCK_RATE_LEN + 2;
56
57         if (ieee->modulation & IEEE80211_OFDM_MODULATION)
58
59                 rate_len += IEEE80211_OFDM_RATE_LEN + 2;
60
61         return rate_len;
62 }
63
64 /* pleace the MFIE rate, tag to the memory (double) poined.
65  * Then it updates the pointer so that
66  * it points after the new MFIE tag added.
67  */
68 void ieee80211_MFIE_Brate(struct ieee80211_device *ieee, u8 **tag_p)
69 {
70         u8 *tag = *tag_p;
71
72         if (ieee->modulation & IEEE80211_CCK_MODULATION){
73                 *tag++ = MFIE_TYPE_RATES;
74                 *tag++ = 4;
75                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_1MB;
76                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_2MB;
77                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_5MB;
78                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_11MB;
79         }
80
81         /* We may add an option for custom rates that specific HW might support */
82         *tag_p = tag;
83 }
84
85 void ieee80211_MFIE_Grate(struct ieee80211_device *ieee, u8 **tag_p)
86 {
87         u8 *tag = *tag_p;
88
89                 if (ieee->modulation & IEEE80211_OFDM_MODULATION){
90
91                 *tag++ = MFIE_TYPE_RATES_EX;
92                 *tag++ = 8;
93                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_6MB;
94                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_9MB;
95                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_12MB;
96                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_18MB;
97                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_24MB;
98                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_36MB;
99                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_48MB;
100                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_54MB;
101
102         }
103
104         /* We may add an option for custom rates that specific HW might support */
105         *tag_p = tag;
106 }
107
108
109 void ieee80211_WMM_Info(struct ieee80211_device *ieee, u8 **tag_p) {
110         u8 *tag = *tag_p;
111
112         *tag++ = MFIE_TYPE_GENERIC; //0
113         *tag++ = 7;
114         *tag++ = 0x00;
115         *tag++ = 0x50;
116         *tag++ = 0xf2;
117         *tag++ = 0x02;//5
118         *tag++ = 0x00;
119         *tag++ = 0x01;
120 #ifdef SUPPORT_USPD
121         if(ieee->current_network.wmm_info & 0x80) {
122                 *tag++ = 0x0f|MAX_SP_Len;
123         } else {
124                 *tag++ = MAX_SP_Len;
125         }
126 #else
127         *tag++ = MAX_SP_Len;
128 #endif
129         *tag_p = tag;
130 }
131
132 #ifdef THOMAS_TURBO
133 void ieee80211_TURBO_Info(struct ieee80211_device *ieee, u8 **tag_p) {
134         u8 *tag = *tag_p;
135
136         *tag++ = MFIE_TYPE_GENERIC; //0
137         *tag++ = 7;
138         *tag++ = 0x00;
139         *tag++ = 0xe0;
140         *tag++ = 0x4c;
141         *tag++ = 0x01;//5
142         *tag++ = 0x02;
143         *tag++ = 0x11;
144         *tag++ = 0x00;
145
146         *tag_p = tag;
147         printk(KERN_ALERT "This is enable turbo mode IE process\n");
148 }
149 #endif
150
151 void enqueue_mgmt(struct ieee80211_device *ieee, struct sk_buff *skb)
152 {
153         int nh;
154         nh = (ieee->mgmt_queue_head +1) % MGMT_QUEUE_NUM;
155
156 /*
157  * if the queue is full but we have newer frames then
158  * just overwrites the oldest.
159  *
160  * if (nh == ieee->mgmt_queue_tail)
161  *              return -1;
162  */
163         ieee->mgmt_queue_head = nh;
164         ieee->mgmt_queue_ring[nh] = skb;
165
166         //return 0;
167 }
168
169 struct sk_buff *dequeue_mgmt(struct ieee80211_device *ieee)
170 {
171         struct sk_buff *ret;
172
173         if(ieee->mgmt_queue_tail == ieee->mgmt_queue_head)
174                 return NULL;
175
176         ret = ieee->mgmt_queue_ring[ieee->mgmt_queue_tail];
177
178         ieee->mgmt_queue_tail =
179                 (ieee->mgmt_queue_tail+1) % MGMT_QUEUE_NUM;
180
181         return ret;
182 }
183
184 void init_mgmt_queue(struct ieee80211_device *ieee)
185 {
186         ieee->mgmt_queue_tail = ieee->mgmt_queue_head = 0;
187 }
188
189
190 void ieee80211_sta_wakeup(struct ieee80211_device *ieee, short nl);
191
192 inline void softmac_mgmt_xmit(struct sk_buff *skb, struct ieee80211_device *ieee)
193 {
194         unsigned long flags;
195         short single = ieee->softmac_features & IEEE_SOFTMAC_SINGLE_QUEUE;
196         struct ieee80211_hdr_3addr  *header=
197                 (struct ieee80211_hdr_3addr  *) skb->data;
198
199
200         spin_lock_irqsave(&ieee->lock, flags);
201
202         /* called with 2nd param 0, no mgmt lock required */
203         ieee80211_sta_wakeup(ieee,0);
204
205         if(single){
206                 if(ieee->queue_stop){
207
208                         enqueue_mgmt(ieee,skb);
209                 }else{
210                         header->seq_ctl = cpu_to_le16(ieee->seq_ctrl[0]<<4);
211
212                         if (ieee->seq_ctrl[0] == 0xFFF)
213                                 ieee->seq_ctrl[0] = 0;
214                         else
215                                 ieee->seq_ctrl[0]++;
216
217                         /* avoid watchdog triggers */
218                         ieee->dev->trans_start = jiffies;
219                         ieee->softmac_data_hard_start_xmit(skb,ieee->dev,ieee->basic_rate);
220                 }
221
222                 spin_unlock_irqrestore(&ieee->lock, flags);
223         }else{
224                 spin_unlock_irqrestore(&ieee->lock, flags);
225                 spin_lock_irqsave(&ieee->mgmt_tx_lock, flags);
226
227                 header->seq_ctl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
228
229                 if (ieee->seq_ctrl[0] == 0xFFF)
230                         ieee->seq_ctrl[0] = 0;
231                 else
232                         ieee->seq_ctrl[0]++;
233
234                 /* avoid watchdog triggers */
235                 ieee->dev->trans_start = jiffies;
236                 ieee->softmac_hard_start_xmit(skb,ieee->dev);
237
238                 spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags);
239         }
240 }
241
242
243 inline void softmac_ps_mgmt_xmit(struct sk_buff *skb, struct ieee80211_device *ieee)
244 {
245
246         short single = ieee->softmac_features & IEEE_SOFTMAC_SINGLE_QUEUE;
247         struct ieee80211_hdr_3addr  *header =
248                 (struct ieee80211_hdr_3addr  *) skb->data;
249
250
251         if(single){
252
253                 header->seq_ctl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
254
255                 if (ieee->seq_ctrl[0] == 0xFFF)
256                         ieee->seq_ctrl[0] = 0;
257                 else
258                         ieee->seq_ctrl[0]++;
259
260                 /* avoid watchdog triggers */
261                 ieee->dev->trans_start = jiffies;
262                 ieee->softmac_data_hard_start_xmit(skb,ieee->dev,ieee->basic_rate);
263
264         }else{
265
266                 header->seq_ctl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
267
268                 if (ieee->seq_ctrl[0] == 0xFFF)
269                         ieee->seq_ctrl[0] = 0;
270                 else
271                         ieee->seq_ctrl[0]++;
272
273                 /* avoid watchdog triggers */
274                 ieee->dev->trans_start = jiffies;
275                 ieee->softmac_hard_start_xmit(skb,ieee->dev);
276
277         }
278 //      dev_kfree_skb_any(skb);//edit by thomas
279 }
280 //by amy for power save
281 inline struct sk_buff *ieee80211_disassociate_skb(
282                                                         struct ieee80211_network *beacon,
283                                                         struct ieee80211_device *ieee,
284                                                         u8      asRsn)
285 {
286         struct sk_buff *skb;
287         struct ieee80211_disassoc_frame *disass;
288
289         skb = dev_alloc_skb(sizeof(struct ieee80211_disassoc_frame));
290         if (!skb)
291                 return NULL;
292
293         disass = (struct ieee80211_disassoc_frame *) skb_put(skb,sizeof(struct ieee80211_disassoc_frame));
294         disass->header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_DISASSOC);
295         disass->header.duration_id = 0;
296
297         memcpy(disass->header.addr1, beacon->bssid, ETH_ALEN);
298         memcpy(disass->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
299         memcpy(disass->header.addr3, beacon->bssid, ETH_ALEN);
300
301         disass->reasoncode = asRsn;
302         return skb;
303 }
304 void
305 SendDisassociation(
306         struct ieee80211_device *ieee,
307         u8*                     asSta,
308         u8                      asRsn
309 )
310 {
311         struct ieee80211_network *beacon = &ieee->current_network;
312         struct sk_buff *skb;
313         skb = ieee80211_disassociate_skb(beacon,ieee,asRsn);
314         if (skb){
315                 softmac_mgmt_xmit(skb, ieee);
316                 //dev_kfree_skb_any(skb);//edit by thomas
317         }
318 }
319
320 //by amy for power save
321 inline struct sk_buff *ieee80211_probe_req(struct ieee80211_device *ieee)
322 {
323         unsigned int len,rate_len;
324         u8 *tag;
325         struct sk_buff *skb;
326         struct ieee80211_probe_request *req;
327
328 #ifdef _RTL8187_EXT_PATCH_
329         short extMore = 0;
330         if(ieee->ext_patch_ieee80211_probe_req_1)
331                 extMore = ieee->ext_patch_ieee80211_probe_req_1(ieee);
332 #endif
333
334         len = ieee->current_network.ssid_len;
335
336         rate_len = ieee80211_MFIE_rate_len(ieee);
337
338 #ifdef _RTL8187_EXT_PATCH_
339         if(!extMore)
340 #endif
341         skb = dev_alloc_skb(sizeof(struct ieee80211_probe_request) +
342                             2 + len + rate_len);
343 #ifdef _RTL8187_EXT_PATCH_
344         else
345                 skb = dev_alloc_skb(sizeof(struct ieee80211_probe_request) +
346                             2 + len + rate_len+128); // MESHID + CAP
347 #endif
348
349         if (!skb)
350                 return NULL;
351
352         req = (struct ieee80211_probe_request *) skb_put(skb,sizeof(struct ieee80211_probe_request));
353         req->header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
354         req->header.duration_id = 0; //FIXME: is this OK ?
355
356         memset(req->header.addr1, 0xff, ETH_ALEN);
357         memcpy(req->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
358         memset(req->header.addr3, 0xff, ETH_ALEN);
359
360         tag = (u8 *) skb_put(skb,len+2+rate_len);
361
362         *tag++ = MFIE_TYPE_SSID;
363         *tag++ = len;
364         memcpy(tag, ieee->current_network.ssid, len);
365         tag += len;
366         ieee80211_MFIE_Brate(ieee,&tag);
367         ieee80211_MFIE_Grate(ieee,&tag);
368
369 #ifdef _RTL8187_EXT_PATCH_
370         if(extMore)
371                 ieee->ext_patch_ieee80211_probe_req_2(ieee, skb, tag);
372 #endif
373         return skb;
374 }
375
376 struct sk_buff *ieee80211_get_beacon_(struct ieee80211_device *ieee);
377
378 void ext_ieee80211_send_beacon_wq(struct ieee80211_device *ieee)
379 {
380         struct sk_buff *skb;
381
382         //unsigned long flags;
383
384         skb = ieee80211_get_beacon_(ieee);
385
386         if (skb){
387                 softmac_mgmt_xmit(skb, ieee);
388                 ieee->softmac_stats.tx_beacons++;
389                 dev_kfree_skb_any(skb);//edit by thomas
390         }
391
392
393         //printk(KERN_WARNING "[1] beacon sending!\n");
394         ieee->beacon_timer.expires = jiffies +
395                 (MSECS( ieee->current_network.beacon_interval -5));
396
397         //spin_lock_irqsave(&ieee->beacon_lock,flags);
398         if(ieee->beacon_txing)
399                 add_timer(&ieee->beacon_timer);
400         //spin_unlock_irqrestore(&ieee->beacon_lock,flags);
401 }
402
403 void ieee80211_send_beacon(struct ieee80211_device *ieee)
404 {
405         struct sk_buff *skb;
406
407         //unsigned long flags;
408
409         skb = ieee80211_get_beacon_(ieee);
410
411         if (skb){
412                 softmac_mgmt_xmit(skb, ieee);
413                 ieee->softmac_stats.tx_beacons++;
414                 dev_kfree_skb_any(skb);//edit by thomas
415         }
416
417         //printk(KERN_WARNING "[1] beacon sending!\n");
418         ieee->beacon_timer.expires = jiffies +
419                 (MSECS( ieee->current_network.beacon_interval -5));
420
421         //spin_lock_irqsave(&ieee->beacon_lock,flags);
422         if(ieee->beacon_txing)
423                 add_timer(&ieee->beacon_timer);
424         //spin_unlock_irqrestore(&ieee->beacon_lock,flags);
425 }
426
427
428 void ieee80211_send_beacon_cb(unsigned long _ieee)
429 {
430         struct ieee80211_device *ieee =
431                 (struct ieee80211_device *) _ieee;
432         unsigned long flags;
433
434         spin_lock_irqsave(&ieee->beacon_lock, flags);
435         ieee80211_send_beacon(ieee);
436         spin_unlock_irqrestore(&ieee->beacon_lock, flags);
437 }
438
439 #ifdef _RTL8187_EXT_PATCH_
440
441 inline struct sk_buff *ieee80211_probe_req_with_SSID(struct ieee80211_device *ieee, char *ssid, int len_ssid)
442 {
443         unsigned int len,rate_len;
444         u8 *tag;
445         struct sk_buff *skb;
446         struct ieee80211_probe_request *req;
447
448 #ifdef _RTL8187_EXT_PATCH_
449         short extMore = 0;
450         if(ieee->ext_patch_ieee80211_probe_req_1)
451                 extMore = ieee->ext_patch_ieee80211_probe_req_1(ieee);
452 #endif
453
454         len = len_ssid;
455
456         rate_len = ieee80211_MFIE_rate_len(ieee);
457
458 #ifdef _RTL8187_EXT_PATCH_
459         if(!extMore)
460 #endif
461         skb = dev_alloc_skb(sizeof(struct ieee80211_probe_request) +
462                             2 + len + rate_len);
463 #ifdef _RTL8187_EXT_PATCH_
464         else
465                 skb = dev_alloc_skb(sizeof(struct ieee80211_probe_request) +
466                             2 + len + rate_len+128); // MESHID + CAP
467 #endif
468
469         if (!skb)
470                 return NULL;
471
472         req = (struct ieee80211_probe_request *) skb_put(skb,sizeof(struct ieee80211_probe_request));
473         req->header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
474         req->header.duration_id = 0; //FIXME: is this OK ?
475
476         memset(req->header.addr1, 0xff, ETH_ALEN);
477         memcpy(req->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
478         memset(req->header.addr3, 0xff, ETH_ALEN);
479
480         tag = (u8 *) skb_put(skb,len+2+rate_len);
481
482         *tag++ = MFIE_TYPE_SSID;
483         *tag++ = len;
484         if(len)
485         {
486                 memcpy(tag, ssid, len);
487                 tag += len;
488         }
489
490         ieee80211_MFIE_Brate(ieee,&tag);
491         ieee80211_MFIE_Grate(ieee,&tag);
492
493 #ifdef _RTL8187_EXT_PATCH_
494         if(extMore)
495                 ieee->ext_patch_ieee80211_probe_req_2(ieee, skb, tag);
496 #endif
497         return skb;
498 }
499
500 #endif // _RTL8187_EXT_PATCH_
501
502
503 void ieee80211_send_probe(struct ieee80211_device *ieee)
504 {
505         struct sk_buff *skb;
506
507 #ifdef _RTL8187_EXT_PATCH_
508         if(ieee->iw_mode == ieee->iw_ext_mode)
509                 skb = ieee80211_probe_req_with_SSID(ieee, NULL, 0);
510         else
511 #endif
512         skb = ieee80211_probe_req(ieee);
513         if (skb){
514                 softmac_mgmt_xmit(skb, ieee);
515                 ieee->softmac_stats.tx_probe_rq++;
516                 //dev_kfree_skb_any(skb);//edit by thomas
517         }
518 }
519
520 void ieee80211_send_probe_requests(struct ieee80211_device *ieee)
521 {
522         if (ieee->active_scan && (ieee->softmac_features & IEEE_SOFTMAC_PROBERQ)){
523                 ieee80211_send_probe(ieee);
524                 ieee80211_send_probe(ieee);
525         }
526 }
527
528 /* this performs syncro scan blocking the caller until all channels
529  * in the allowed channel map has been checked.
530  */
531 void ieee80211_softmac_scan_syncro(struct ieee80211_device *ieee)
532 {
533         short ch = 0;
534 #ifdef ENABLE_DOT11D
535         u8 channel_map[MAX_CHANNEL_NUMBER+1];
536         memcpy(channel_map, GET_DOT11D_INFO(ieee)->channel_map, MAX_CHANNEL_NUMBER+1);
537 #endif
538         down(&ieee->scan_sem);
539 //      printk("==================> Sync scan\n");
540 //      dump_chnl_map(channel_map);
541
542         while(1)
543         {
544
545                 do{
546                         ch++;
547                         if (ch > MAX_CHANNEL_NUMBER)
548                                 goto out; /* scan completed */
549
550 #ifdef ENABLE_DOT11D
551                 }while(!channel_map[ch]);
552 #else
553                 }while(!ieee->channel_map[ch]);
554 #endif
555                 /* this fuction can be called in two situations
556                  * 1- We have switched to ad-hoc mode and we are
557                  *    performing a complete syncro scan before conclude
558                  *    there are no interesting cell and to create a
559                  *    new one. In this case the link state is
560                  *    IEEE80211_NOLINK until we found an interesting cell.
561                  *    If so the ieee8021_new_net, called by the RX path
562                  *    will set the state to IEEE80211_LINKED, so we stop
563                  *    scanning
564                  * 2- We are linked and the root uses run iwlist scan.
565                  *    So we switch to IEEE80211_LINKED_SCANNING to remember
566                  *    that we are still logically linked (not interested in
567                  *    new network events, despite for updating the net list,
568                  *    but we are temporarly 'unlinked' as the driver shall
569                  *    not filter RX frames and the channel is changing.
570                  * So the only situation in witch are interested is to check
571                  * if the state become LINKED because of the #1 situation
572                  */
573
574                 if (ieee->state == IEEE80211_LINKED)
575                         goto out;
576
577                 ieee->set_chan(ieee->dev, ch);
578 //              printk("=====>channel=%d   ",ch);
579 #ifdef ENABLE_DOT11D
580                 if(channel_map[ch] == 1)
581 #endif
582                 {
583 //                      printk("====send probe request\n");
584                         ieee80211_send_probe_requests(ieee);
585                 }
586                 /* this prevent excessive time wait when we
587                  * need to wait for a syncro scan to end..
588                  */
589                 if (ieee->sync_scan_hurryup)
590                         goto out;
591
592
593                 msleep_interruptible_rtl(IEEE80211_SOFTMAC_SCAN_TIME);
594
595         }
596 out:
597         ieee->sync_scan_hurryup = 0;
598         up(&ieee->scan_sem);
599 #ifdef ENABLE_DOT11D
600         if(IS_DOT11D_ENABLE(ieee))
601                 DOT11D_ScanComplete(ieee);
602 #endif
603 }
604
605 void ieee80211_softmac_ips_scan_syncro(struct ieee80211_device *ieee)
606 {
607         int ch;
608         unsigned int watch_dog = 0;
609 #ifdef ENABLE_DOT11D
610         u8 channel_map[MAX_CHANNEL_NUMBER+1];
611         memcpy(channel_map, GET_DOT11D_INFO(ieee)->channel_map, MAX_CHANNEL_NUMBER+1);
612 #endif
613         down(&ieee->scan_sem);
614         ch = ieee->current_network.channel;
615 //              if(ieee->sync_scan_hurryup)
616 //      {
617
618 //              printk("stop scan sync\n");
619 //              goto out;
620 //      }
621 //      printk("=======hh===============>ips scan\n");
622         while(1)
623         {
624                 /* this fuction can be called in two situations
625                  * 1- We have switched to ad-hoc mode and we are
626                  *    performing a complete syncro scan before conclude
627                  *    there are no interesting cell and to create a
628                  *    new one. In this case the link state is
629                  *    IEEE80211_NOLINK until we found an interesting cell.
630                  *    If so the ieee8021_new_net, called by the RX path
631                  *    will set the state to IEEE80211_LINKED, so we stop
632                  *    scanning
633                  * 2- We are linked and the root uses run iwlist scan.
634                  *    So we switch to IEEE80211_LINKED_SCANNING to remember
635                  *    that we are still logically linked (not interested in
636                  *    new network events, despite for updating the net list,
637                  *    but we are temporarly 'unlinked' as the driver shall
638                  *    not filter RX frames and the channel is changing.
639                  * So the only situation in witch are interested is to check
640                  * if the state become LINKED because of the #1 situation
641                  */
642                 if (ieee->state == IEEE80211_LINKED)
643                 {
644                         goto out;
645                 }
646 #ifdef ENABLE_DOT11D
647                 if(channel_map[ieee->current_network.channel] > 0)
648 #endif
649                 {
650                         ieee->set_chan(ieee->dev, ieee->current_network.channel);
651 //                      printk("======>channel=%d  ",ieee->current_network.channel);
652                 }
653 #ifdef ENABLE_DOT11D
654                 if(channel_map[ieee->current_network.channel] == 1)
655 #endif
656                 {
657 //                      printk("====send probe request\n");
658                         ieee80211_send_probe_requests(ieee);
659                 }
660                 /* this prevent excessive time wait when we
661                  * need to wait for a syncro scan to end..
662                  */
663 //                if (ieee->sync_scan_hurryup)
664 //                        goto out;
665
666                 msleep_interruptible_rtl(IEEE80211_SOFTMAC_SCAN_TIME);
667
668                 do{
669                         if (watch_dog++ >= MAX_CHANNEL_NUMBER)
670                 //      if (++watch_dog >= 15);//MAX_CHANNEL_NUMBER)  //YJ,modified,080630
671                                 goto out; /* scan completed */
672
673                         ieee->current_network.channel = (ieee->current_network.channel + 1)%MAX_CHANNEL_NUMBER;
674 #ifdef ENABLE_DOT11D
675                 }while(!channel_map[ieee->current_network.channel]);
676 #else
677                 }while(!ieee->channel_map[ieee->current_network.channel]);
678 #endif
679         }
680 out:
681         //ieee->sync_scan_hurryup = 0;
682         //ieee->set_chan(ieee->dev, ch);
683         //ieee->current_network.channel = ch;
684         ieee->actscanning = false;
685         up(&ieee->scan_sem);
686 #ifdef ENABLE_DOT11D
687         if(IS_DOT11D_ENABLE(ieee))
688                 DOT11D_ScanComplete(ieee);
689 #endif
690 }
691
692
693 #if 0
694 /* called both by wq with ieee->lock held */
695 void ieee80211_softmac_scan(struct ieee80211_device *ieee)
696 {
697         short watchdog = 0;
698
699         do{
700                 ieee->current_network.channel =
701                         (ieee->current_network.channel + 1) % MAX_CHANNEL_NUMBER;
702                 if (watchdog++ > MAX_CHANNEL_NUMBER)
703                                 return; /* no good chans */
704
705         }while(!ieee->channel_map[ieee->current_network.channel]);
706
707
708         schedule_work(&ieee->softmac_scan_wq);
709 }
710 #endif
711 #ifdef ENABLE_IPS
712 void ieee80211_softmac_scan_wq(struct work_struct *work)
713 {
714         struct delayed_work *dwork = to_delayed_work(work);
715         struct ieee80211_device *ieee = container_of(dwork, struct ieee80211_device, softmac_scan_wq);
716         static short watchdog = 0;
717 #ifdef ENABLE_DOT11D
718         u8 channel_map[MAX_CHANNEL_NUMBER+1];
719         memcpy(channel_map, GET_DOT11D_INFO(ieee)->channel_map, MAX_CHANNEL_NUMBER+1);
720 #endif
721 //      printk("ieee80211_softmac_scan_wq ENABLE_IPS\n");
722 //      printk("in %s\n",__func__);
723         down(&ieee->scan_sem);
724
725         do{
726                 ieee->current_network.channel =
727                         (ieee->current_network.channel + 1) % MAX_CHANNEL_NUMBER;
728                 if (watchdog++ > MAX_CHANNEL_NUMBER)
729                                 goto out; /* no good chans */
730
731 #ifdef ENABLE_DOT11D
732         }while(!channel_map[ieee->current_network.channel]);
733 #else
734         }while(!ieee->channel_map[ieee->current_network.channel]);
735 #endif
736
737         //printk("current_network.channel:%d\n", ieee->current_network.channel);
738         if (ieee->scanning == 0 )
739         {
740                 printk("error out, scanning = 0\n");
741                 goto out;
742         }
743         ieee->set_chan(ieee->dev, ieee->current_network.channel);
744 #ifdef ENABLE_DOT11D
745         if(channel_map[ieee->current_network.channel] == 1)
746 #endif
747                 ieee80211_send_probe_requests(ieee);
748
749         queue_delayed_work(ieee->wq, &ieee->softmac_scan_wq, IEEE80211_SOFTMAC_SCAN_TIME);
750         up(&ieee->scan_sem);
751         return;
752 out:
753         ieee->actscanning = false;
754         watchdog = 0;
755         ieee->scanning = 0;
756         up(&ieee->scan_sem);
757
758 #ifdef ENABLE_DOT11D
759         if(IS_DOT11D_ENABLE(ieee))
760                 DOT11D_ScanComplete(ieee);
761 #endif
762         return;
763 }
764 #else
765 void ieee80211_softmac_scan_wq(struct work_struct *work)
766 {
767         struct delayed_work *dwork = to_delayed_work(work);
768         struct ieee80211_device *ieee = container_of(work, struct ieee80211_device, softmac_scan_wq);
769         short watchdog = 0;
770 #ifdef ENABLE_DOT11D
771         u8 channel_map[MAX_CHANNEL_NUMBER+1];
772         memcpy(channel_map, GET_DOT11D_INFO(ieee)->channel_map, MAX_CHANNEL_NUMBER+1);
773 #endif
774 //      printk("enter scan wq,watchdog is %d\n",watchdog);
775         down(&ieee->scan_sem);
776
777         do{
778                 ieee->current_network.channel =
779                         (ieee->current_network.channel + 1) % MAX_CHANNEL_NUMBER;
780                 if (watchdog++ > MAX_CHANNEL_NUMBER)
781                                 goto out; /* no good chans */
782
783 #ifdef ENABLE_DOT11D
784         }while(!channel_map[ieee->current_network.channel]);
785 #else
786         }while(!ieee->channel_map[ieee->current_network.channel]);
787 #endif
788
789 //      printk("current_network.channel:%d\n", ieee->current_network.channel);
790         if (ieee->scanning == 0 )
791         {
792                 printk("error out, scanning = 0\n");
793                 goto out;
794         }
795         ieee->set_chan(ieee->dev, ieee->current_network.channel);
796 #ifdef ENABLE_DOT11D
797         if(channel_map[ieee->current_network.channel] == 1)
798 #endif
799                 ieee80211_send_probe_requests(ieee);
800
801         queue_delayed_work(ieee->wq, &ieee->softmac_scan_wq, IEEE80211_SOFTMAC_SCAN_TIME);
802 out:
803         up(&ieee->scan_sem);
804 #ifdef ENABLE_DOT11D
805         if(IS_DOT11D_ENABLE(ieee))
806                 DOT11D_ScanComplete(ieee);
807 #endif
808 }
809
810 #endif
811
812 void ieee80211_beacons_start(struct ieee80211_device *ieee)
813 {
814         unsigned long flags;
815
816         spin_lock_irqsave(&ieee->beacon_lock,flags);
817
818         ieee->beacon_txing = 1;
819         ieee80211_send_beacon(ieee);
820
821         spin_unlock_irqrestore(&ieee->beacon_lock,flags);
822 }
823
824 void ieee80211_beacons_stop(struct ieee80211_device *ieee)
825 {
826         unsigned long flags;
827
828         spin_lock_irqsave(&ieee->beacon_lock,flags);
829
830         ieee->beacon_txing = 0;
831         del_timer_sync(&ieee->beacon_timer);
832
833         spin_unlock_irqrestore(&ieee->beacon_lock,flags);
834
835 }
836
837
838 void ieee80211_stop_send_beacons(struct ieee80211_device *ieee)
839 {
840         if(ieee->stop_send_beacons)
841                 ieee->stop_send_beacons(ieee->dev);
842         if (ieee->softmac_features & IEEE_SOFTMAC_BEACONS)
843                 ieee80211_beacons_stop(ieee);
844 }
845
846
847 void ieee80211_start_send_beacons(struct ieee80211_device *ieee)
848 {
849         if(ieee->start_send_beacons)
850                 ieee->start_send_beacons(ieee->dev);
851         if(ieee->softmac_features & IEEE_SOFTMAC_BEACONS)
852                 ieee80211_beacons_start(ieee);
853 }
854
855
856 void ieee80211_softmac_stop_scan(struct ieee80211_device *ieee)
857 {
858 //      unsigned long flags;
859
860         //ieee->sync_scan_hurryup = 1;
861
862         down(&ieee->scan_sem);
863 //      spin_lock_irqsave(&ieee->lock, flags);
864
865         if (ieee->scanning == 1){
866                 ieee->scanning = 0;
867                 //del_timer_sync(&ieee->scan_timer);
868                 cancel_delayed_work(&ieee->softmac_scan_wq);
869         }
870
871 //      spin_unlock_irqrestore(&ieee->lock, flags);
872         up(&ieee->scan_sem);
873 }
874
875 void ieee80211_stop_scan(struct ieee80211_device *ieee)
876 {
877         if (ieee->softmac_features & IEEE_SOFTMAC_SCAN)
878                 ieee80211_softmac_stop_scan(ieee);
879         else
880                 ieee->stop_scan(ieee->dev);
881 }
882
883 /* called with ieee->lock held */
884 void ieee80211_start_scan(struct ieee80211_device *ieee)
885 {
886 #ifdef ENABLE_DOT11D
887         if(IS_DOT11D_ENABLE(ieee) )
888         {
889                 if(IS_COUNTRY_IE_VALID(ieee))
890                 {
891                         RESET_CIE_WATCHDOG(ieee);
892                 }
893         }
894 #endif
895         if (ieee->softmac_features & IEEE_SOFTMAC_SCAN){
896                 if (ieee->scanning == 0)
897                 {
898                         ieee->scanning = 1;
899                         //ieee80211_softmac_scan(ieee);
900                 //      queue_work(ieee->wq, &ieee->softmac_scan_wq);
901                 //care this,1203,2007,by lawrence
902 #if 1
903                         queue_delayed_work(ieee->wq, &ieee->softmac_scan_wq,0);
904 #endif
905                 }
906         }else
907                 ieee->start_scan(ieee->dev);
908
909 }
910
911 /* called with wx_sem held */
912 void ieee80211_start_scan_syncro(struct ieee80211_device *ieee)
913 {
914 #ifdef ENABLE_DOT11D
915         if(IS_DOT11D_ENABLE(ieee) )
916         {
917                 if(IS_COUNTRY_IE_VALID(ieee))
918                 {
919                         RESET_CIE_WATCHDOG(ieee);
920                 }
921         }
922 #endif
923         ieee->sync_scan_hurryup = 0;
924
925         if (ieee->softmac_features & IEEE_SOFTMAC_SCAN)
926                 ieee80211_softmac_scan_syncro(ieee);
927         else
928                 ieee->scan_syncro(ieee->dev);
929
930 }
931
932 inline struct sk_buff *ieee80211_authentication_req(struct ieee80211_network *beacon,
933         struct ieee80211_device *ieee, int challengelen)
934 {
935         struct sk_buff *skb;
936         struct ieee80211_authentication *auth;
937
938         skb = dev_alloc_skb(sizeof(struct ieee80211_authentication) + challengelen);
939
940         if (!skb) return NULL;
941
942         auth = (struct ieee80211_authentication *)
943                 skb_put(skb, sizeof(struct ieee80211_authentication));
944
945         auth->header.frame_ctl = IEEE80211_STYPE_AUTH;
946         if (challengelen) auth->header.frame_ctl |= IEEE80211_FCTL_WEP;
947
948         auth->header.duration_id = 0x013a; //FIXME
949
950         memcpy(auth->header.addr1, beacon->bssid, ETH_ALEN);
951         memcpy(auth->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
952         memcpy(auth->header.addr3, beacon->bssid, ETH_ALEN);
953
954         auth->algorithm = ieee->open_wep ? WLAN_AUTH_OPEN : WLAN_AUTH_SHARED_KEY;
955
956         auth->transaction = cpu_to_le16(ieee->associate_seq);
957         ieee->associate_seq++;
958
959         auth->status = cpu_to_le16(WLAN_STATUS_SUCCESS);
960
961         return skb;
962
963 }
964
965 static struct sk_buff* ieee80211_probe_resp(struct ieee80211_device *ieee, u8 *dest)
966 {
967         u8 *tag;
968         int beacon_size;
969         struct ieee80211_probe_response *beacon_buf;
970         struct sk_buff *skb;
971         int encrypt;
972         int atim_len,erp_len;
973         struct ieee80211_crypt_data* crypt;
974
975         char *ssid = ieee->current_network.ssid;
976         int ssid_len = ieee->current_network.ssid_len;
977         int rate_len = ieee->current_network.rates_len+2;
978         int rate_ex_len = ieee->current_network.rates_ex_len;
979         int wpa_ie_len = ieee->wpa_ie_len;
980         if(rate_ex_len > 0) rate_ex_len+=2;
981
982         if(ieee->current_network.capability & WLAN_CAPABILITY_IBSS)
983                 atim_len = 4;
984         else
985                 atim_len = 0;
986
987         if(ieee80211_is_54g(ieee->current_network))
988                 erp_len = 3;
989         else
990                 erp_len = 0;
991
992         beacon_size = sizeof(struct ieee80211_probe_response)+
993                 ssid_len
994                 +3 //channel
995                 +rate_len
996                 +rate_ex_len
997                 +atim_len
998                 +wpa_ie_len
999                 +erp_len;
1000
1001         skb = dev_alloc_skb(beacon_size);
1002
1003         if (!skb)
1004                 return NULL;
1005
1006         beacon_buf = (struct ieee80211_probe_response*) skb_put(skb, beacon_size);
1007
1008         memcpy (beacon_buf->header.addr1, dest,ETH_ALEN);
1009         memcpy (beacon_buf->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
1010         memcpy (beacon_buf->header.addr3, ieee->current_network.bssid, ETH_ALEN);
1011
1012         beacon_buf->header.duration_id = 0; //FIXME
1013         beacon_buf->beacon_interval =
1014                 cpu_to_le16(ieee->current_network.beacon_interval);
1015         beacon_buf->capability =
1016                 cpu_to_le16(ieee->current_network.capability & WLAN_CAPABILITY_IBSS);
1017
1018         if(ieee->short_slot && (ieee->current_network.capability & WLAN_CAPABILITY_SHORT_SLOT))
1019                 cpu_to_le16((beacon_buf->capability |= WLAN_CAPABILITY_SHORT_SLOT));
1020
1021         crypt = ieee->crypt[ieee->tx_keyidx];
1022
1023         encrypt = ieee->host_encrypt && crypt && crypt->ops &&
1024                 ((0 == strcmp(crypt->ops->name, "WEP")) || wpa_ie_len);
1025
1026         if (encrypt)
1027                 beacon_buf->capability |= cpu_to_le16(WLAN_CAPABILITY_PRIVACY);
1028
1029
1030         beacon_buf->header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_PROBE_RESP);
1031
1032         beacon_buf->info_element.id = MFIE_TYPE_SSID;
1033         beacon_buf->info_element.len = ssid_len;
1034
1035         tag = (u8*) beacon_buf->info_element.data;
1036
1037         memcpy(tag, ssid, ssid_len);
1038
1039         tag += ssid_len;
1040
1041         *(tag++) = MFIE_TYPE_RATES;
1042         *(tag++) = rate_len-2;
1043         memcpy(tag,ieee->current_network.rates,rate_len-2);
1044         tag+=rate_len-2;
1045
1046         *(tag++) = MFIE_TYPE_DS_SET;
1047         *(tag++) = 1;
1048         *(tag++) = ieee->current_network.channel;
1049
1050         if(atim_len){
1051                 *(tag++) = MFIE_TYPE_IBSS_SET;
1052                 *(tag++) = 2;
1053                 *((u16*)(tag)) = cpu_to_le16(ieee->current_network.atim_window);
1054                 tag+=2;
1055         }
1056
1057         if(erp_len){
1058                 *(tag++) = MFIE_TYPE_ERP;
1059                 *(tag++) = 1;
1060                 *(tag++) = 0;
1061         }
1062
1063         if(rate_ex_len){
1064                 *(tag++) = MFIE_TYPE_RATES_EX;
1065                 *(tag++) = rate_ex_len-2;
1066                 memcpy(tag,ieee->current_network.rates_ex,rate_ex_len-2);
1067                 tag+=rate_ex_len-2;
1068         }
1069
1070         if (wpa_ie_len)
1071         {
1072                 if (ieee->iw_mode == IW_MODE_ADHOC)
1073                 {//as Windows will set pairwise key same as the group key which is not allowed in Linux, so set this for IOT issue. WB 2008.07.07
1074                         memcpy(&ieee->wpa_ie[14], &ieee->wpa_ie[8], 4);
1075                 }
1076
1077                 memcpy(tag, ieee->wpa_ie, ieee->wpa_ie_len);
1078         }
1079
1080         skb->dev = ieee->dev;
1081         return skb;
1082 }
1083 #ifdef _RTL8187_EXT_PATCH_
1084 struct sk_buff* ieee80211_ext_probe_resp_by_net(struct ieee80211_device *ieee, u8 *dest, struct ieee80211_network *net)
1085 {
1086         u8 *tag;
1087         int beacon_size;
1088         struct ieee80211_probe_response *beacon_buf;
1089         struct sk_buff *skb;
1090         int encrypt;
1091         int atim_len,erp_len;
1092         struct ieee80211_crypt_data* crypt;
1093         u8 broadcast_addr[] = {0xff,0xff,0xff,0xff,0xff,0xff};
1094         int wpa_ie_len = ieee->wpa_ie_len;
1095         char *ssid = net->ssid;
1096         int ssid_len = net->ssid_len;
1097
1098         int rate_len = ieee->current_network.rates_len+2;
1099         int rate_ex_len = ieee->current_network.rates_ex_len;
1100         if(rate_ex_len > 0) rate_ex_len+=2;
1101
1102         if( ieee->meshScanMode&4)
1103                 ieee->current_network.channel = ieee->ext_patch_ieee80211_ext_stop_scan_wq_set_channel(ieee);
1104         if( ieee->meshScanMode&6)
1105                 queue_work(ieee->wq, &ieee->ext_stop_scan_wq);
1106
1107         if(ieee->current_network.capability & WLAN_CAPABILITY_IBSS) // use current_network here
1108                 atim_len = 4;
1109         else
1110                 atim_len = 0;
1111
1112         if(ieee80211_is_54g(*net))
1113                 erp_len = 3;
1114         else
1115                 erp_len = 0;
1116
1117         beacon_size = sizeof(struct ieee80211_probe_response)+
1118                 ssid_len
1119                 +3 //channel
1120                 +rate_len
1121                 +rate_ex_len
1122                 +atim_len
1123                 +erp_len;
1124 //b
1125         skb = dev_alloc_skb(beacon_size+196);
1126
1127         if (!skb)
1128                 return NULL;
1129
1130         beacon_buf = (struct ieee80211_probe_response*) skb_put(skb, beacon_size);
1131
1132         memcpy (beacon_buf->header.addr1, dest,ETH_ALEN);
1133         memcpy (beacon_buf->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
1134         memcpy (beacon_buf->header.addr3, ieee->current_network.bssid, ETH_ALEN);
1135
1136         beacon_buf->header.duration_id = 0; //FIXME
1137
1138         beacon_buf->beacon_interval =
1139                 cpu_to_le16(ieee->current_network.beacon_interval);  // use current_network here
1140         beacon_buf->capability =
1141                 cpu_to_le16(ieee->current_network.capability & WLAN_CAPABILITY_IBSS);
1142
1143         if(ieee->short_slot && (ieee->current_network.capability & WLAN_CAPABILITY_SHORT_SLOT))
1144                 cpu_to_le16((beacon_buf->capability |= WLAN_CAPABILITY_SHORT_SLOT));
1145
1146         crypt = ieee->crypt[ieee->tx_keyidx];
1147
1148         encrypt = ieee->host_encrypt && crypt && crypt->ops &&
1149                 ((0 == strcmp(crypt->ops->name, "WEP"))||wpa_ie_len);
1150
1151         if (encrypt)
1152                 beacon_buf->capability |= cpu_to_le16(WLAN_CAPABILITY_PRIVACY);
1153
1154
1155         beacon_buf->header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_PROBE_RESP);
1156
1157         beacon_buf->info_element.id = MFIE_TYPE_SSID;
1158         beacon_buf->info_element.len = ssid_len;
1159
1160         tag = (u8*) beacon_buf->info_element.data;
1161
1162         // brocad cast / probe rsp
1163         if(memcmp(dest, broadcast_addr, ETH_ALEN ))
1164                 memcpy(tag, ssid, ssid_len);
1165         else
1166                 ssid_len=0;
1167
1168         tag += ssid_len;
1169
1170 //get_bssrate_set(priv, _SUPPORTEDRATES_IE_, &pbssrate, &bssrate_len);
1171 //pbuf = set_ie(pbuf, _SUPPORTEDRATES_IE_, bssrate_len, pbssrate, &frlen);
1172
1173         *(tag++) = MFIE_TYPE_RATES;
1174         *(tag++) = rate_len-2;
1175         memcpy(tag,ieee->current_network.rates,rate_len-2);
1176         tag+=rate_len-2;
1177
1178         *(tag++) = MFIE_TYPE_DS_SET;
1179         *(tag++) = 1;
1180         *(tag++) = ieee->current_network.channel;  // use current_network here
1181
1182
1183         if(atim_len){
1184                 *(tag++) = MFIE_TYPE_IBSS_SET;
1185                 *(tag++) = 2;
1186                 *((u16*)(tag)) = cpu_to_le16(ieee->current_network.atim_window); // use current_network here
1187                 tag+=2;
1188         }
1189
1190         if(erp_len){
1191                 *(tag++) = MFIE_TYPE_ERP;
1192                 *(tag++) = 1;
1193                 *(tag++) = 0;
1194         }
1195
1196         if(rate_ex_len){
1197                 *(tag++) = MFIE_TYPE_RATES_EX;
1198                 *(tag++) = rate_ex_len-2;
1199                 memcpy(tag,ieee->current_network.rates_ex,rate_ex_len-2);
1200                 tag+=rate_ex_len-2;
1201         }
1202         if (wpa_ie_len)
1203                 memcpy(tag, ieee->wpa_ie, ieee->wpa_ie_len);
1204
1205         skb->dev = ieee->dev;
1206         return skb;
1207 }
1208 #endif // _RTL8187_EXT_PATCH_
1209
1210 struct sk_buff* ieee80211_assoc_resp(struct ieee80211_device *ieee, u8 *dest)
1211 {
1212         struct sk_buff *skb;
1213         u8* tag;
1214
1215         struct ieee80211_crypt_data* crypt;
1216         struct ieee80211_assoc_response_frame *assoc;
1217         short encrypt;
1218
1219         unsigned int rate_len = ieee80211_MFIE_rate_len(ieee);
1220         int len = sizeof(struct ieee80211_assoc_response_frame) + rate_len;
1221
1222         skb = dev_alloc_skb(len);
1223
1224         if (!skb)
1225                 return NULL;
1226
1227         assoc = (struct ieee80211_assoc_response_frame *)
1228                 skb_put(skb,sizeof(struct ieee80211_assoc_response_frame));
1229
1230         assoc->header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP);
1231         memcpy(assoc->header.addr1, dest,ETH_ALEN);
1232         memcpy(assoc->header.addr3, ieee->dev->dev_addr, ETH_ALEN);
1233         memcpy(assoc->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
1234         assoc->capability = cpu_to_le16(ieee->iw_mode == IW_MODE_MASTER ?
1235                 WLAN_CAPABILITY_BSS : WLAN_CAPABILITY_IBSS);
1236
1237
1238         if(ieee->short_slot)
1239                 assoc->capability |= cpu_to_le16(WLAN_CAPABILITY_SHORT_SLOT);
1240
1241         if (ieee->host_encrypt)
1242                 crypt = ieee->crypt[ieee->tx_keyidx];
1243         else crypt = NULL;
1244
1245         encrypt = ( crypt && crypt->ops);
1246
1247         if (encrypt)
1248                 assoc->capability |= cpu_to_le16(WLAN_CAPABILITY_PRIVACY);
1249
1250         assoc->status = 0;
1251         assoc->aid = cpu_to_le16(ieee->assoc_id);
1252         if (ieee->assoc_id == 0x2007) ieee->assoc_id=0;
1253         else ieee->assoc_id++;
1254
1255         tag = (u8*) skb_put(skb, rate_len);
1256
1257         ieee80211_MFIE_Brate(ieee, &tag);
1258         ieee80211_MFIE_Grate(ieee, &tag);
1259
1260         return skb;
1261 }
1262
1263 struct sk_buff* ieee80211_auth_resp(struct ieee80211_device *ieee,int status, u8 *dest)
1264 {
1265         struct sk_buff *skb;
1266         struct ieee80211_authentication *auth;
1267
1268         skb = dev_alloc_skb(sizeof(struct ieee80211_authentication)+1);
1269
1270         if (!skb)
1271                 return NULL;
1272
1273         skb->len = sizeof(struct ieee80211_authentication);
1274
1275         auth = (struct ieee80211_authentication *)skb->data;
1276
1277         auth->status = cpu_to_le16(status);
1278         auth->transaction = cpu_to_le16(2);
1279         auth->algorithm = cpu_to_le16(WLAN_AUTH_OPEN);
1280
1281 #ifdef _RTL8187_EXT_PATCH_
1282         if(ieee->iw_mode == ieee->iw_ext_mode)
1283                 memcpy(auth->header.addr3, dest, ETH_ALEN);
1284 #else
1285         memcpy(auth->header.addr3, ieee->dev->dev_addr, ETH_ALEN);
1286 #endif
1287         memcpy(auth->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
1288         memcpy(auth->header.addr1, dest, ETH_ALEN);
1289         auth->header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_AUTH);
1290         return skb;
1291
1292
1293 }
1294
1295 struct sk_buff* ieee80211_null_func(struct ieee80211_device *ieee,short pwr)
1296 {
1297         struct sk_buff *skb;
1298         struct ieee80211_hdr_3addr* hdr;
1299
1300         skb = dev_alloc_skb(sizeof(struct ieee80211_hdr_3addr));
1301
1302         if (!skb)
1303                 return NULL;
1304
1305         hdr = (struct ieee80211_hdr_3addr*)skb_put(skb,sizeof(struct ieee80211_hdr_3addr));
1306
1307         memcpy(hdr->addr1, ieee->current_network.bssid, ETH_ALEN);
1308         memcpy(hdr->addr2, ieee->dev->dev_addr, ETH_ALEN);
1309         memcpy(hdr->addr3, ieee->current_network.bssid, ETH_ALEN);
1310
1311         hdr->frame_ctl = cpu_to_le16(IEEE80211_FTYPE_DATA |
1312                 IEEE80211_STYPE_NULLFUNC | IEEE80211_FCTL_TODS |
1313                 (pwr ? IEEE80211_FCTL_PM:0));
1314
1315         return skb;
1316
1317
1318 }
1319
1320
1321 void ieee80211_resp_to_assoc_rq(struct ieee80211_device *ieee, u8* dest)
1322 {
1323         struct sk_buff *buf = ieee80211_assoc_resp(ieee, dest);
1324
1325         if (buf){
1326                 softmac_mgmt_xmit(buf, ieee);
1327                 dev_kfree_skb_any(buf);//edit by thomas
1328         }
1329 }
1330
1331
1332 void ieee80211_resp_to_auth(struct ieee80211_device *ieee, int s, u8* dest)
1333 {
1334         struct sk_buff *buf = ieee80211_auth_resp(ieee, s, dest);
1335
1336         if (buf){
1337                 softmac_mgmt_xmit(buf, ieee);
1338                 dev_kfree_skb_any(buf);//edit by thomas
1339         }
1340 }
1341
1342
1343 void ieee80211_resp_to_probe(struct ieee80211_device *ieee, u8 *dest)
1344 {
1345
1346         struct sk_buff *buf = ieee80211_probe_resp(ieee, dest);
1347
1348         if (buf) {
1349                 softmac_mgmt_xmit(buf, ieee);
1350                 dev_kfree_skb_any(buf);//edit by thomas
1351         }
1352 }
1353
1354
1355 inline struct sk_buff *ieee80211_association_req(struct ieee80211_network *beacon,struct ieee80211_device *ieee)
1356 {
1357         struct sk_buff *skb;
1358         //unsigned long flags;
1359
1360         struct ieee80211_assoc_request_frame *hdr;
1361         u8 *tag;
1362         //short info_addr = 0;
1363         //int i;
1364         //u16 suite_count = 0;
1365         //u8 suit_select = 0;
1366         unsigned int wpa_len = beacon->wpa_ie_len;
1367         //struct net_device *dev = ieee->dev;
1368         //union iwreq_data wrqu;
1369         //u8 *buff;
1370         //u8 *p;
1371 #if 1
1372         // for testing purpose
1373         unsigned int rsn_len = beacon->rsn_ie_len;
1374 #else
1375         unsigned int rsn_len = beacon->rsn_ie_len - 4;
1376 #endif
1377         unsigned int rate_len = ieee80211_MFIE_rate_len(ieee);
1378         unsigned int wmm_info_len = beacon->QoS_Enable?9:0;
1379 #ifdef THOMAS_TURBO
1380         unsigned int turbo_info_len = beacon->Turbo_Enable?9:0;
1381 #endif
1382
1383         u8  encry_proto = ieee->wpax_type_notify & 0xff;
1384         //u8  pairwise_type = (ieee->wpax_type_notify >> 8) & 0xff;
1385         //u8  authen_type = (ieee->wpax_type_notify >> 16) & 0xff;
1386
1387         int len = 0;
1388
1389         //[0] Notify type of encryption: WPA/WPA2
1390         //[1] pair wise type
1391         //[2] authen type
1392         if(ieee->wpax_type_set) {
1393                 if (IEEE_PROTO_WPA == encry_proto) {
1394                         rsn_len = 0;
1395                 } else if (IEEE_PROTO_RSN == encry_proto) {
1396                         wpa_len = 0;
1397                 }
1398         }
1399 #ifdef THOMAS_TURBO
1400         len = sizeof(struct ieee80211_assoc_request_frame)+
1401                 + beacon->ssid_len//essid tagged val
1402                 + rate_len//rates tagged val
1403                 + wpa_len
1404                 + rsn_len
1405                 + wmm_info_len
1406                 + turbo_info_len;
1407 #else
1408         len = sizeof(struct ieee80211_assoc_request_frame)+
1409                 + beacon->ssid_len//essid tagged val
1410                 + rate_len//rates tagged val
1411                 + wpa_len
1412                 + rsn_len
1413                 + wmm_info_len;
1414 #endif
1415
1416 #ifdef _RTL8187_EXT_PATCH_
1417         if(ieee->iw_mode == ieee->iw_ext_mode)
1418                 skb = dev_alloc_skb(len+256); // stanley
1419         else
1420 #endif
1421         skb = dev_alloc_skb(len);
1422
1423         if (!skb)
1424                 return NULL;
1425
1426         hdr = (struct ieee80211_assoc_request_frame *)
1427                 skb_put(skb, sizeof(struct ieee80211_assoc_request_frame));
1428
1429
1430         hdr->header.frame_ctl = IEEE80211_STYPE_ASSOC_REQ;
1431         hdr->header.duration_id= 37; //FIXME
1432         memcpy(hdr->header.addr1, beacon->bssid, ETH_ALEN);
1433         memcpy(hdr->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
1434         memcpy(hdr->header.addr3, beacon->bssid, ETH_ALEN);
1435         memcpy(ieee->ap_mac_addr, beacon->bssid, ETH_ALEN);//for HW security, John
1436
1437         hdr->capability = cpu_to_le16(WLAN_CAPABILITY_BSS);
1438         if (beacon->capability & WLAN_CAPABILITY_PRIVACY )
1439                 hdr->capability |= cpu_to_le16(WLAN_CAPABILITY_PRIVACY);
1440         if (beacon->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
1441                 hdr->capability |= cpu_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE);
1442
1443         if(ieee->short_slot)
1444                 hdr->capability |= cpu_to_le16(WLAN_CAPABILITY_SHORT_SLOT);
1445
1446 #ifdef _RTL8187_EXT_PATCH_
1447         if((ieee->iw_mode == ieee->iw_ext_mode) && ieee->ext_patch_ieee80211_association_req_1)
1448                 ieee->ext_patch_ieee80211_association_req_1(hdr);
1449 #endif
1450
1451         hdr->listen_interval = 0xa; //FIXME
1452
1453         hdr->info_element.id = MFIE_TYPE_SSID;
1454
1455         hdr->info_element.len = beacon->ssid_len;
1456         tag = skb_put(skb, beacon->ssid_len);
1457         memcpy(tag, beacon->ssid, beacon->ssid_len);
1458
1459         tag = skb_put(skb, rate_len);
1460
1461         ieee80211_MFIE_Brate(ieee, &tag);
1462         ieee80211_MFIE_Grate(ieee, &tag);
1463
1464         //add rsn==0 condition for ap's mix security mode(wpa+wpa2), john2007.8.9
1465         //choose AES encryption as default algorithm while using mixed mode
1466 #if 0
1467         if(rsn_len == 0){
1468
1469                 tag = skb_put(skb,wpa_len);
1470
1471                 if(wpa_len) {
1472
1473
1474                   //{add by david. 2006.8.31
1475                   //fix linksys compatibility bug
1476                   //}
1477                   if(wpa_len > 24) {//22+2, mean include the capability
1478                         beacon->wpa_ie[wpa_len - 2] = 0;
1479                   }
1480                 //multicast cipher OUI
1481                   if(  beacon->wpa_ie[11]==0x2      ){ //0x0050f202 is the oui of tkip
1482                   ieee->broadcast_key_type = KEY_TYPE_TKIP;
1483                 }
1484                   else if(  beacon->wpa_ie[11]==0x4      ){//0x0050f204 is the oui of ccmp
1485                   ieee->broadcast_key_type = KEY_TYPE_CCMP;
1486                 }
1487                 //unicast cipher OUI
1488                   if(   beacon->wpa_ie[14]==0
1489                         && beacon->wpa_ie[15]==0x50
1490                         && beacon->wpa_ie[16]==0xf2
1491                         && beacon->wpa_ie[17]==0x2      ){ //0x0050f202 is the oui of tkip
1492                   ieee->pairwise_key_type = KEY_TYPE_TKIP;
1493                 }
1494
1495                   else if(   beacon->wpa_ie[14]==0
1496                         && beacon->wpa_ie[15]==0x50
1497                         && beacon->wpa_ie[16]==0xf2
1498                         && beacon->wpa_ie[17]==0x4      ){//0x0050f204 is the oui of ccmp
1499                   ieee->pairwise_key_type = KEY_TYPE_CCMP;
1500                 }
1501                 //indicate the wpa_ie content to WPA_SUPPLICANT
1502                 buff = kmalloc(IW_CUSTOM_MAX, GFP_ATOMIC);
1503                 memset(buff, 0, IW_CUSTOM_MAX);
1504                 p=buff;
1505                 p += sprintf(p, "ASSOCINFO(ReqIEs=");
1506                 for(i=0;i<wpa_len;i++){
1507                         p += sprintf(p, "%02x", beacon->wpa_ie[i]);
1508                 }
1509                 p += sprintf(p, ")");
1510                 memset(&wrqu, 0, sizeof(wrqu) );
1511                 wrqu.data.length = p - buff;
1512
1513                 wireless_send_event(dev, IWEVCUSTOM, &wrqu, buff);
1514                   memcpy(tag,beacon->wpa_ie,wpa_len);
1515                 }
1516
1517         }
1518
1519         if(rsn_len > 22) {
1520
1521                                                 if(     beacon->rsn_ie[4]==0x0 &&
1522                                 beacon->rsn_ie[5]==0xf &&
1523                                 beacon->rsn_ie[6]==0xac){
1524
1525                                 switch(beacon->rsn_ie[7]){
1526                                         case 0x1:
1527                                                 ieee->broadcast_key_type = KEY_TYPE_WEP40;
1528                                                 break;
1529                                         case 0x2:
1530                                                 ieee->broadcast_key_type = KEY_TYPE_TKIP;
1531                                                 break;
1532                                         case 0x4:
1533                                                 ieee->broadcast_key_type = KEY_TYPE_CCMP;
1534                                                 break;
1535                                         case 0x5:
1536                                                 ieee->broadcast_key_type = KEY_TYPE_WEP104;
1537                                                 break;
1538                                         default:
1539                                                 printk("fault suite type in RSN broadcast key\n");
1540                                                 break;
1541                                 }
1542                         }
1543
1544                         if(     beacon->rsn_ie[10]==0x0 &&
1545                                 beacon->rsn_ie[11]==0xf &&
1546                                 beacon->rsn_ie[12]==0xac){
1547                                 if(beacon->rsn_ie[8]==1){//not mixed mode
1548                                         switch(beacon->rsn_ie[13]){
1549                                                 case 0x2:
1550                                                         ieee->pairwise_key_type = KEY_TYPE_TKIP;
1551                                                         break;
1552                                                 case 0x4:
1553                                                         ieee->pairwise_key_type = KEY_TYPE_CCMP;
1554                                                         break;
1555                                                 default:
1556                                                         printk("fault suite type in RSN pairwise key\n");
1557                                                         break;
1558                                         }
1559                                 }
1560                                 else if(beacon->rsn_ie[8]==2){//mixed mode
1561                                         ieee->pairwise_key_type = KEY_TYPE_CCMP;
1562                                 }
1563                         }
1564
1565
1566
1567                 tag = skb_put(skb,22);
1568                 memcpy(tag,(beacon->rsn_ie + info_addr),8);
1569                 tag[1] =  20;
1570                 tag += 8;
1571                 info_addr += 8;
1572
1573                 spin_lock_irqsave(&ieee->wpax_suitlist_lock,flags);
1574                 for (i = 0; i < 2; i++) {
1575                         tag[0] = 1;
1576                         tag[1] = 0;
1577                         tag += 2;
1578                         suite_count = beacon->rsn_ie[info_addr] + \
1579                                       (beacon->rsn_ie[info_addr + 1] << 8);
1580                         info_addr += 2;
1581                         if(1 == suite_count) {
1582                                 memcpy(tag,(beacon->rsn_ie + info_addr),4);
1583                                 info_addr += 4;
1584                         } else {
1585                                 // if the wpax_type_notify has been set by the application,
1586                                 // just use it, otherwise just use the default one.
1587                                 if(ieee->wpax_type_set) {
1588                                         suit_select = ((0 == i) ? pairwise_type:authen_type)&0x0f ;
1589                                         memcpy(tag,rsn_authen_cipher_suite[suit_select],4);
1590                                 } else {
1591                                         //default set as ccmp, or none authentication
1592                                         if(i == 0) {
1593                                                 memcpy(tag,rsn_authen_cipher_suite[4],4);
1594                                         } else {
1595                                                 memcpy(tag,rsn_authen_cipher_suite[2],4);
1596                                         }
1597
1598                                 }
1599
1600                                 info_addr += (suite_count * 4);
1601                         }
1602                         tag += 4;
1603                 }
1604                 spin_unlock_irqrestore(&ieee->wpax_suitlist_lock,flags);
1605
1606                 tag[0] = 0;
1607                 tag[1] = beacon->rsn_ie[info_addr+1];
1608
1609         } else {
1610                 tag = skb_put(skb,rsn_len);
1611                 if(rsn_len) {
1612
1613
1614                         if(     beacon->rsn_ie[4]==0x0 &&
1615                                 beacon->rsn_ie[5]==0xf &&
1616                                 beacon->rsn_ie[6]==0xac){
1617                                 switch(beacon->rsn_ie[7]){
1618                                         case 0x1:
1619                                                 ieee->broadcast_key_type = KEY_TYPE_WEP40;
1620                                                 break;
1621                                         case 0x2:
1622                                                 ieee->broadcast_key_type = KEY_TYPE_TKIP;
1623                                                 break;
1624                                         case 0x4:
1625                                                 ieee->broadcast_key_type = KEY_TYPE_CCMP;
1626                                                 break;
1627                                         case 0x5:
1628                                                 ieee->broadcast_key_type = KEY_TYPE_WEP104;
1629                                                 break;
1630                                         default:
1631                                                 printk("fault suite type in RSN broadcast key\n");
1632                                                 break;
1633                                 }
1634                         }
1635                         if(     beacon->rsn_ie[10]==0x0 &&
1636                                 beacon->rsn_ie[11]==0xf &&
1637                                 beacon->rsn_ie[12]==0xac){
1638                                 if(beacon->rsn_ie[8]==1){//not mixed mode
1639                                         switch(beacon->rsn_ie[13]){
1640                                                 case 0x2:
1641                                                         ieee->pairwise_key_type = KEY_TYPE_TKIP;
1642                                                         break;
1643                                                 case 0x4:
1644                                                         ieee->pairwise_key_type = KEY_TYPE_CCMP;
1645                                                         break;
1646                                                 default:
1647                                                         printk("fault suite type in RSN pairwise key\n");
1648                                                         break;
1649                                         }
1650
1651                                 }
1652                                 else if(beacon->rsn_ie[8]==2){//mixed mode
1653                                         ieee->pairwise_key_type = KEY_TYPE_CCMP;
1654                                 }
1655                         }
1656
1657
1658                         beacon->rsn_ie[rsn_len - 2] = 0;
1659                         memcpy(tag,beacon->rsn_ie,rsn_len);
1660                 }
1661         }
1662 #else
1663         tag = skb_put(skb,ieee->wpa_ie_len);
1664         memcpy(tag,ieee->wpa_ie,ieee->wpa_ie_len);
1665 #endif
1666         tag = skb_put(skb,wmm_info_len);
1667         if(wmm_info_len) {
1668           ieee80211_WMM_Info(ieee, &tag);
1669         }
1670 #ifdef THOMAS_TURBO
1671         tag = skb_put(skb,turbo_info_len);
1672         if(turbo_info_len) {
1673                 ieee80211_TURBO_Info(ieee, &tag);
1674         }
1675 #endif
1676
1677 #ifdef _RTL8187_EXT_PATCH_
1678         if((ieee->iw_mode == ieee->iw_ext_mode) && ieee->ext_patch_ieee80211_association_req_2)
1679                 ieee->ext_patch_ieee80211_association_req_2(ieee, beacon, skb);
1680 #endif
1681
1682         return skb;
1683 }
1684
1685 void ieee80211_associate_abort(struct ieee80211_device *ieee)
1686 {
1687
1688         unsigned long flags;
1689         spin_lock_irqsave(&ieee->lock, flags);
1690
1691         ieee->associate_seq++;
1692
1693         /* don't scan, and avoid to have the RX path possibily
1694          * try again to associate. Even do not react to AUTH or
1695          * ASSOC response. Just wait for the retry wq to be scheduled.
1696          * Here we will check if there are good nets to associate
1697          * with, so we retry or just get back to NO_LINK and scanning
1698          */
1699         if (ieee->state == IEEE80211_ASSOCIATING_AUTHENTICATING){
1700                 IEEE80211_DEBUG_MGMT("Authentication failed\n");
1701                 ieee->softmac_stats.no_auth_rs++;
1702         }else{
1703                 IEEE80211_DEBUG_MGMT("Association failed\n");
1704                 ieee->softmac_stats.no_ass_rs++;
1705         }
1706
1707         ieee->state = IEEE80211_ASSOCIATING_RETRY;
1708
1709         queue_delayed_work(ieee->wq, &ieee->associate_retry_wq,IEEE80211_SOFTMAC_ASSOC_RETRY_TIME);
1710
1711         spin_unlock_irqrestore(&ieee->lock, flags);
1712 }
1713
1714 void ieee80211_associate_abort_cb(unsigned long dev)
1715 {
1716         ieee80211_associate_abort((struct ieee80211_device *) dev);
1717 }
1718
1719
1720 void ieee80211_associate_step1(struct ieee80211_device *ieee)
1721 {
1722         struct ieee80211_network *beacon = &ieee->current_network;
1723         struct sk_buff *skb;
1724
1725         IEEE80211_DEBUG_MGMT("Stopping scan\n");
1726         ieee->softmac_stats.tx_auth_rq++;
1727         skb=ieee80211_authentication_req(beacon, ieee, 0);
1728 #ifdef _RTL8187_EXT_PATCH_
1729         if(ieee->iw_mode == ieee->iw_ext_mode ) {
1730                 if(skb)
1731                         softmac_mgmt_xmit(skb, ieee);
1732                 return;
1733         }else
1734 #endif
1735         if (!skb){
1736
1737                 ieee80211_associate_abort(ieee);
1738         }
1739         else{
1740                 ieee->state = IEEE80211_ASSOCIATING_AUTHENTICATING ;
1741                 IEEE80211_DEBUG_MGMT("Sending authentication request\n");
1742                 //printk("---Sending authentication request\n");
1743                 softmac_mgmt_xmit(skb, ieee);
1744                 //BUGON when you try to add_timer twice, using mod_timer may be better, john0709
1745                 if(!timer_pending(&ieee->associate_timer)){
1746                         ieee->associate_timer.expires = jiffies + (HZ / 2);
1747                         add_timer(&ieee->associate_timer);
1748                 }
1749                 //If call dev_kfree_skb_any,a warning will ocur....
1750                 //KERNEL: assertion (!atomic_read(&skb->users)) failed at net/core/dev.c (1708)
1751                 //So ... 1204 by lawrence.
1752                 //printk("\nIn %s,line %d call kfree skb.",__func__,__LINE__);
1753                 //dev_kfree_skb_any(skb);//edit by thomas
1754         }
1755 }
1756
1757 void ieee80211_auth_challenge(struct ieee80211_device *ieee, u8 *challenge, int chlen)
1758 {
1759         u8 *c;
1760         struct sk_buff *skb;
1761         struct ieee80211_network *beacon = &ieee->current_network;
1762 //      int hlen = sizeof(struct ieee80211_authentication);
1763         del_timer_sync(&ieee->associate_timer);
1764         ieee->associate_seq++;
1765         ieee->softmac_stats.tx_auth_rq++;
1766
1767         skb = ieee80211_authentication_req(beacon, ieee, chlen+2);
1768         if (!skb)
1769                 ieee80211_associate_abort(ieee);
1770         else{
1771                 c = skb_put(skb, chlen+2);
1772                 *(c++) = MFIE_TYPE_CHALLENGE;
1773                 *(c++) = chlen;
1774                 memcpy(c, challenge, chlen);
1775
1776                 IEEE80211_DEBUG_MGMT("Sending authentication challenge response\n");
1777
1778                 ieee80211_encrypt_fragment(ieee, skb, sizeof(struct ieee80211_hdr_3addr  ));
1779
1780                 softmac_mgmt_xmit(skb, ieee);
1781                 if (!timer_pending(&ieee->associate_timer)){
1782                 //printk("=========>add timer again, to crash\n");
1783                 ieee->associate_timer.expires = jiffies + (HZ / 2);
1784                 add_timer(&ieee->associate_timer);
1785                 }
1786                 dev_kfree_skb_any(skb);//edit by thomas
1787         }
1788         kfree(challenge);
1789 }
1790
1791 #ifdef _RTL8187_EXT_PATCH_
1792
1793 // based on ieee80211_assoc_resp
1794 struct sk_buff* ieee80211_assoc_resp_by_net(struct ieee80211_device *ieee, u8 *dest, unsigned short status, struct ieee80211_network *pstat, int pkt_type)
1795 {
1796         struct sk_buff *skb;
1797         u8* tag;
1798
1799         struct ieee80211_crypt_data* crypt;
1800         struct ieee80211_assoc_response_frame *assoc;
1801         short encrypt;
1802
1803         unsigned int rate_len = ieee80211_MFIE_rate_len(ieee);
1804         int len = sizeof(struct ieee80211_assoc_response_frame) + rate_len;
1805
1806         if(ieee->iw_mode == ieee->iw_ext_mode)
1807                 skb = dev_alloc_skb(len+256); // stanley
1808         else
1809                 skb = dev_alloc_skb(len);
1810
1811         if (!skb)
1812                 return NULL;
1813
1814         assoc = (struct ieee80211_assoc_response_frame *)
1815                 skb_put(skb,sizeof(struct ieee80211_assoc_response_frame));
1816
1817         assoc->header.frame_ctl = cpu_to_le16(pkt_type);
1818
1819         memcpy(assoc->header.addr1, dest,ETH_ALEN);
1820         memcpy(assoc->header.addr3, ieee->dev->dev_addr, ETH_ALEN);
1821         memcpy(assoc->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
1822         assoc->capability = cpu_to_le16(ieee->iw_mode == IW_MODE_MASTER ?
1823                 WLAN_CAPABILITY_BSS : WLAN_CAPABILITY_IBSS);
1824
1825         if((ieee->iw_mode == ieee->iw_ext_mode) && ieee->ext_patch_ieee80211_assoc_resp_by_net_1)
1826                 ieee->ext_patch_ieee80211_assoc_resp_by_net_1(assoc);
1827
1828         if(ieee->short_slot)
1829                 assoc->capability |= cpu_to_le16(WLAN_CAPABILITY_SHORT_SLOT);
1830
1831         if (ieee->host_encrypt)
1832                 crypt = ieee->crypt[ieee->tx_keyidx];
1833         else crypt = NULL;
1834
1835         encrypt = ( crypt && crypt->ops);
1836
1837         if (encrypt)
1838                 assoc->capability |= cpu_to_le16(WLAN_CAPABILITY_PRIVACY);
1839
1840         assoc->status = 0;
1841         assoc->aid = cpu_to_le16(ieee->assoc_id);
1842         if (ieee->assoc_id == 0x2007) ieee->assoc_id=0;
1843         else ieee->assoc_id++;
1844
1845         assoc->info_element.id = 230; // Stanley, an unused id (just a hot fix)
1846         assoc->info_element.len = 0;
1847
1848         tag = (u8*) skb_put(skb, rate_len);
1849
1850         ieee80211_MFIE_Brate(ieee, &tag);
1851         ieee80211_MFIE_Grate(ieee, &tag);
1852
1853         if((ieee->iw_mode == ieee->iw_ext_mode) && ieee->ext_patch_ieee80211_assoc_resp_by_net_2)
1854                 ieee->ext_patch_ieee80211_assoc_resp_by_net_2(ieee, pstat, pkt_type, skb);
1855
1856         return skb;
1857 }
1858
1859 // based on ieee80211_resp_to_assoc_rq
1860 void ieee80211_ext_issue_assoc_rsp(struct ieee80211_device *ieee, u8 *dest, unsigned short status, struct ieee80211_network *pstat, int pkt_type)
1861 {
1862         struct sk_buff *buf = ieee80211_assoc_resp_by_net(ieee, dest, status, pstat, pkt_type);
1863
1864         if (buf)
1865                 softmac_mgmt_xmit(buf, ieee);
1866 }
1867
1868 // based on ieee80211_associate_step2
1869 void ieee80211_ext_issue_assoc_req(struct ieee80211_device *ieee, struct ieee80211_network *pstat)
1870 {
1871
1872         struct sk_buff* skb;
1873
1874         // printk("@@@@@ ieee80211_ext_issue_assoc_req on channel: %d\n", ieee->current_network.channel);
1875
1876         ieee->softmac_stats.tx_ass_rq++;
1877         skb=ieee80211_association_req(pstat, ieee);
1878         if (skb)
1879                 softmac_mgmt_xmit(skb, ieee);
1880 }
1881
1882 void ieee80211_ext_issue_disassoc(struct ieee80211_device *ieee, struct ieee80211_network *pstat, int reason, unsigned char extReason)
1883 {
1884         // do nothing
1885         // printk("@@@@@ ieee80211_ext_issue_disassoc\n");
1886         return;
1887 }
1888 #endif // _RTL8187_EXT_PATCH_
1889
1890 void ieee80211_associate_step2(struct ieee80211_device *ieee)
1891 {
1892         struct sk_buff* skb;
1893         struct ieee80211_network *beacon = &ieee->current_network;
1894
1895         del_timer_sync(&ieee->associate_timer);
1896
1897         IEEE80211_DEBUG_MGMT("Sending association request\n");
1898         ieee->softmac_stats.tx_ass_rq++;
1899         skb=ieee80211_association_req(beacon, ieee);
1900         if (!skb)
1901                 ieee80211_associate_abort(ieee);
1902         else{
1903                 softmac_mgmt_xmit(skb, ieee);
1904                 if (!timer_pending(&ieee->associate_timer)){
1905                 ieee->associate_timer.expires = jiffies + (HZ / 2);
1906                 add_timer(&ieee->associate_timer);
1907                 }
1908                 //dev_kfree_skb_any(skb);//edit by thomas
1909         }
1910 }
1911
1912 void ieee80211_associate_complete_wq(struct work_struct *work)
1913 {
1914         struct ieee80211_device *ieee = container_of(work, struct ieee80211_device, associate_complete_wq);
1915
1916         printk(KERN_INFO "Associated successfully\n");
1917         if(ieee80211_is_54g(ieee->current_network) &&
1918                 (ieee->modulation & IEEE80211_OFDM_MODULATION)){
1919
1920                 ieee->rate = 540;
1921                 printk(KERN_INFO"Using G rates\n");
1922         }else{
1923                 ieee->rate = 110;
1924                 printk(KERN_INFO"Using B rates\n");
1925         }
1926         ieee->link_change(ieee->dev);
1927         notify_wx_assoc_event(ieee);
1928         if (ieee->data_hard_resume)
1929                 ieee->data_hard_resume(ieee->dev);
1930         netif_carrier_on(ieee->dev);
1931 }
1932
1933 void ieee80211_associate_complete(struct ieee80211_device *ieee)
1934 {
1935         int i;
1936         del_timer_sync(&ieee->associate_timer);
1937
1938         for(i = 0; i < 6; i++) {
1939           //ieee->seq_ctrl[i] = 0;
1940         }
1941         ieee->state = IEEE80211_LINKED;
1942         IEEE80211_DEBUG_MGMT("Successfully associated\n");
1943
1944         queue_work(ieee->wq, &ieee->associate_complete_wq);
1945 }
1946
1947 void ieee80211_associate_procedure_wq(struct work_struct *work)
1948 {
1949         struct ieee80211_device *ieee = container_of(work, struct ieee80211_device, associate_procedure_wq);
1950
1951         ieee->sync_scan_hurryup = 1;
1952         down(&ieee->wx_sem);
1953
1954         if (ieee->data_hard_stop)
1955                 ieee->data_hard_stop(ieee->dev);
1956
1957         ieee80211_stop_scan(ieee);
1958         ieee->set_chan(ieee->dev, ieee->current_network.channel);
1959
1960         ieee->associate_seq = 1;
1961         ieee80211_associate_step1(ieee);
1962
1963         up(&ieee->wx_sem);
1964 }
1965 #ifdef _RTL8187_EXT_PATCH_
1966 // based on ieee80211_associate_procedure_wq
1967
1968 void ieee80211_ext_stop_scan_wq(struct work_struct *work)
1969 {
1970         struct ieee80211_device *ieee = container_of(work, struct ieee80211_device, ext_stop_scan_wq);
1971
1972         if (ieee->scanning == 0) {
1973                 if((ieee->iw_mode == ieee->iw_ext_mode) && ieee->ext_patch_ieee80211_ext_stop_scan_wq_set_channel
1974                                 && ( ieee->current_network.channel == ieee->ext_patch_ieee80211_ext_stop_scan_wq_set_channel(ieee) ) )
1975                 return;
1976         }
1977
1978         ieee->sync_scan_hurryup = 1;
1979
1980         down(&ieee->wx_sem);
1981
1982         // printk("@@@@@@@@@@ ieee80211_ext_stop_scan_wq\n");
1983         if (ieee->data_hard_stop)
1984                 ieee->data_hard_stop(ieee->dev);
1985
1986         ieee80211_stop_scan(ieee);
1987
1988         // set channel
1989         if((ieee->iw_mode == ieee->iw_ext_mode) && ieee->ext_patch_ieee80211_ext_stop_scan_wq_set_channel)
1990                 ieee->set_chan(ieee->dev, ieee->ext_patch_ieee80211_ext_stop_scan_wq_set_channel(ieee));
1991         else
1992                 ieee->set_chan(ieee->dev, ieee->current_network.channel);
1993         //
1994         up(&ieee->wx_sem);
1995 }
1996
1997
1998 void ieee80211_ext_send_11s_beacon(struct ieee80211_device *ieee)
1999 {
2000         queue_work(ieee->wq, &ieee->ext_send_beacon_wq);
2001 }
2002
2003 #endif // _RTL8187_EXT_PATCH_
2004
2005 inline void ieee80211_softmac_new_net(struct ieee80211_device *ieee, struct ieee80211_network *net)
2006 {
2007         u8 tmp_ssid[IW_ESSID_MAX_SIZE+1];
2008         int tmp_ssid_len = 0;
2009
2010         short apset,ssidset,ssidbroad,apmatch,ssidmatch;
2011
2012         /* we are interested in new new only if we are not associated
2013          * and we are not associating / authenticating
2014          */
2015         if (ieee->state != IEEE80211_NOLINK)
2016                 return;
2017
2018         if ((ieee->iw_mode == IW_MODE_INFRA) && !(net->capability & WLAN_CAPABILITY_BSS))
2019                 return;
2020
2021         if ((ieee->iw_mode == IW_MODE_ADHOC) && !(net->capability & WLAN_CAPABILITY_IBSS))
2022                 return;
2023
2024
2025         if (ieee->iw_mode == IW_MODE_INFRA || ieee->iw_mode == IW_MODE_ADHOC){
2026                 /* if the user specified the AP MAC, we need also the essid
2027                  * This could be obtained by beacons or, if the network does not
2028                  * broadcast it, it can be put manually.
2029                  */
2030                 apset = ieee->wap_set;//(memcmp(ieee->current_network.bssid, zero,ETH_ALEN)!=0 );
2031                 ssidset = ieee->ssid_set;//ieee->current_network.ssid[0] != '\0';
2032                 ssidbroad =  !(net->ssid_len == 0 || net->ssid[0]== '\0');
2033                 apmatch = (memcmp(ieee->current_network.bssid, net->bssid, ETH_ALEN)==0);
2034
2035                 if(ieee->current_network.ssid_len != net->ssid_len)
2036                         ssidmatch = 0;
2037                 else
2038                         ssidmatch = (0==strncmp(ieee->current_network.ssid, net->ssid, net->ssid_len));
2039
2040                 //printk("cur: %s, %d, net:%s, %d\n", ieee->current_network.ssid, ieee->current_network.ssid_len, net->ssid, net->ssid_len);
2041                 //printk("apset=%d apmatch=%d ssidset=%d ssidbroad=%d ssidmatch=%d\n",apset,apmatch,ssidset,ssidbroad,ssidmatch);
2042
2043                 if (    /* if the user set the AP check if match.
2044                          * if the network does not broadcast essid we check the user supplyed ANY essid
2045                          * if the network does broadcast and the user does not set essid it is OK
2046                          * if the network does broadcast and the user did set essid chech if essid match
2047                          */
2048                                 ( apset && apmatch &&
2049                                   ((ssidset && ssidbroad && ssidmatch) || (ssidbroad && !ssidset) || (!ssidbroad && ssidset)) ) ||
2050                                 /* if the ap is not set, check that the user set the bssid
2051                                  * and the network does bradcast and that those two bssid matches
2052                                  */
2053                                 (!apset && ssidset && ssidbroad && ssidmatch)
2054                    ){
2055
2056
2057                         /* if the essid is hidden replace it with the
2058                          * essid provided by the user.
2059                          */
2060                         if (!ssidbroad){
2061                                 strncpy(tmp_ssid, ieee->current_network.ssid, IW_ESSID_MAX_SIZE);
2062                                 tmp_ssid_len = ieee->current_network.ssid_len;
2063                         }
2064                         memcpy(&ieee->current_network, net, sizeof(struct ieee80211_network));
2065
2066                         if (!ssidbroad){
2067                                 strncpy(ieee->current_network.ssid, tmp_ssid, IW_ESSID_MAX_SIZE);
2068                                 ieee->current_network.ssid_len = tmp_ssid_len;
2069                         }
2070                         printk(KERN_INFO"Linking with %s: channel is %d\n",ieee->current_network.ssid,ieee->current_network.channel);
2071
2072                         if (ieee->iw_mode == IW_MODE_INFRA){
2073                                 ieee->state = IEEE80211_ASSOCIATING;
2074                                 ieee->beinretry = false;
2075                                 queue_work(ieee->wq, &ieee->associate_procedure_wq);
2076                         }else{
2077                                 if(ieee80211_is_54g(ieee->current_network) &&
2078                                                 (ieee->modulation & IEEE80211_OFDM_MODULATION)){
2079                                         ieee->rate = 540;
2080                                         printk(KERN_INFO"Using G rates\n");
2081                                 }else{
2082                                         ieee->rate = 110;
2083                                         printk(KERN_INFO"Using B rates\n");
2084                                 }
2085                                 ieee->state = IEEE80211_LINKED;
2086                                 ieee->beinretry = false;
2087                         }
2088
2089                 }
2090         }
2091
2092 }
2093
2094 void ieee80211_softmac_check_all_nets(struct ieee80211_device *ieee)
2095 {
2096         unsigned long flags;
2097         struct ieee80211_network *target;
2098
2099         spin_lock_irqsave(&ieee->lock, flags);
2100         list_for_each_entry(target, &ieee->network_list, list) {
2101
2102                 /* if the state become different that NOLINK means
2103                  * we had found what we are searching for
2104                  */
2105
2106                 if (ieee->state != IEEE80211_NOLINK)
2107                         break;
2108
2109                 if (ieee->scan_age == 0 || time_after(target->last_scanned + ieee->scan_age, jiffies))
2110                         ieee80211_softmac_new_net(ieee, target);
2111         }
2112
2113         spin_unlock_irqrestore(&ieee->lock, flags);
2114
2115 }
2116
2117
2118 static inline u16 auth_parse(struct sk_buff *skb, u8** challenge, int *chlen)
2119 {
2120         struct ieee80211_authentication *a;
2121         u8 *t;
2122         if (skb->len <  (sizeof(struct ieee80211_authentication)-sizeof(struct ieee80211_info_element))){
2123                 IEEE80211_DEBUG_MGMT("invalid len in auth resp: %d\n",skb->len);
2124                 return 0xcafe;
2125         }
2126         *challenge = NULL;
2127         a = (struct ieee80211_authentication*) skb->data;
2128         if(skb->len > (sizeof(struct ieee80211_authentication) +3)){
2129                 t = skb->data + sizeof(struct ieee80211_authentication);
2130
2131                 if(*(t++) == MFIE_TYPE_CHALLENGE){
2132                         *chlen = *(t++);
2133                         *challenge = (u8*)kmalloc(*chlen, GFP_ATOMIC);
2134                         memcpy(*challenge, t, *chlen);
2135                 }
2136         }
2137
2138         return cpu_to_le16(a->status);
2139
2140 }
2141
2142
2143 int auth_rq_parse(struct sk_buff *skb,u8* dest)
2144 {
2145         struct ieee80211_authentication *a;
2146
2147         if (skb->len <  (sizeof(struct ieee80211_authentication)-sizeof(struct ieee80211_info_element))){
2148                 IEEE80211_DEBUG_MGMT("invalid len in auth request: %d\n",skb->len);
2149                 return -1;
2150         }
2151         a = (struct ieee80211_authentication*) skb->data;
2152
2153         memcpy(dest,a->header.addr2, ETH_ALEN);
2154
2155         if (le16_to_cpu(a->algorithm) != WLAN_AUTH_OPEN)
2156                 return  WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
2157
2158         return WLAN_STATUS_SUCCESS;
2159 }
2160
2161 static short probe_rq_parse(struct ieee80211_device *ieee, struct sk_buff *skb, u8 *src)
2162 {
2163         u8 *tag;
2164         u8 *skbend;
2165         u8 *ssid=NULL;
2166         u8 ssidlen = 0;
2167
2168         struct ieee80211_hdr_3addr   *header =
2169                 (struct ieee80211_hdr_3addr   *) skb->data;
2170
2171         if (skb->len < sizeof (struct ieee80211_hdr_3addr  ))
2172                 return -1; /* corrupted */
2173
2174         memcpy(src,header->addr2, ETH_ALEN);
2175
2176         skbend = (u8*)skb->data + skb->len;
2177
2178         tag = skb->data + sizeof (struct ieee80211_hdr_3addr  );
2179
2180         while (tag+1 < skbend){
2181                 if (*tag == 0){
2182                         ssid = tag+2;
2183                         ssidlen = *(tag+1);
2184                         break;
2185                 }
2186                 tag++; /* point to the len field */
2187                 tag = tag + *(tag); /* point to the last data byte of the tag */
2188                 tag++; /* point to the next tag */
2189         }
2190
2191         //IEEE80211DMESG("Card MAC address is "MACSTR, MAC2STR(src));
2192         if (ssidlen == 0) return 1;
2193
2194         if (!ssid) return 1; /* ssid not found in tagged param */
2195         return (!strncmp(ssid, ieee->current_network.ssid, ssidlen));
2196
2197 }
2198
2199 int assoc_rq_parse(struct sk_buff *skb,u8* dest)
2200 {
2201         struct ieee80211_assoc_request_frame *a;
2202
2203         if (skb->len < (sizeof(struct ieee80211_assoc_request_frame) -
2204                 sizeof(struct ieee80211_info_element))) {
2205
2206                 IEEE80211_DEBUG_MGMT("invalid len in auth request:%d \n", skb->len);
2207                 return -1;
2208         }
2209
2210         a = (struct ieee80211_assoc_request_frame*) skb->data;
2211
2212         memcpy(dest,a->header.addr2,ETH_ALEN);
2213
2214         return 0;
2215 }
2216
2217 static inline u16 assoc_parse(struct sk_buff *skb, int *aid)
2218 {
2219         struct ieee80211_assoc_response_frame *a;
2220         if (skb->len <  sizeof(struct ieee80211_assoc_response_frame)){
2221                 IEEE80211_DEBUG_MGMT("invalid len in auth resp: %d\n", skb->len);
2222                 return 0xcafe;
2223         }
2224
2225         a = (struct ieee80211_assoc_response_frame*) skb->data;
2226         *aid = le16_to_cpu(a->aid) & 0x3fff;
2227         return le16_to_cpu(a->status);
2228 }
2229
2230 static inline void
2231 ieee80211_rx_probe_rq(struct ieee80211_device *ieee, struct sk_buff *skb)
2232 {
2233         u8 dest[ETH_ALEN];
2234
2235         //IEEE80211DMESG("Rx probe");
2236         ieee->softmac_stats.rx_probe_rq++;
2237         //DMESG("Dest is "MACSTR, MAC2STR(dest));
2238         if (probe_rq_parse(ieee, skb, dest)){
2239                 //IEEE80211DMESG("Was for me!");
2240                 ieee->softmac_stats.tx_probe_rs++;
2241                 ieee80211_resp_to_probe(ieee, dest);
2242         }
2243 }
2244
2245 inline void
2246 ieee80211_rx_auth_rq(struct ieee80211_device *ieee, struct sk_buff *skb)
2247 {
2248         u8 dest[ETH_ALEN];
2249         int status;
2250         //IEEE80211DMESG("Rx probe");
2251         ieee->softmac_stats.rx_auth_rq++;
2252
2253         if ((status = auth_rq_parse(skb, dest))!= -1){
2254                 ieee80211_resp_to_auth(ieee, status, dest);
2255         }
2256         //DMESG("Dest is "MACSTR, MAC2STR(dest));
2257
2258 }
2259
2260  inline void
2261 ieee80211_rx_assoc_rq(struct ieee80211_device *ieee, struct sk_buff *skb)
2262 {
2263
2264         u8 dest[ETH_ALEN];
2265         //unsigned long flags;
2266
2267         ieee->softmac_stats.rx_ass_rq++;
2268         if (assoc_rq_parse(skb,dest) != -1){
2269                 ieee80211_resp_to_assoc_rq(ieee, dest);
2270         }
2271
2272         printk(KERN_INFO"New client associated: "MAC_FMT"\n", MAC_ARG(dest));
2273         //FIXME
2274         #if 0
2275         spin_lock_irqsave(&ieee->lock,flags);
2276         add_associate(ieee,dest);
2277         spin_unlock_irqrestore(&ieee->lock,flags);
2278         #endif
2279 }
2280
2281
2282
2283 void ieee80211_sta_ps_send_null_frame(struct ieee80211_device *ieee, short pwr)
2284 {
2285
2286         struct sk_buff *buf = ieee80211_null_func(ieee, pwr);
2287
2288         if (buf)
2289                 softmac_ps_mgmt_xmit(buf, ieee);
2290
2291 }
2292
2293
2294 short ieee80211_sta_ps_sleep(struct ieee80211_device *ieee, u32 *time_h, u32 *time_l)
2295 {
2296 #if 0
2297         int timeout = ieee->ps_timeout;
2298 #else
2299         int timeout = 0;
2300 #endif
2301         u8 dtim;
2302         /*if(ieee->ps == IEEE80211_PS_DISABLED ||
2303                 ieee->iw_mode != IW_MODE_INFRA ||
2304                 ieee->state != IEEE80211_LINKED)
2305
2306                 return 0;
2307         */
2308         dtim = ieee->current_network.dtim_data;
2309         //printk("DTIM\n");
2310
2311         if(!(dtim & IEEE80211_DTIM_VALID))
2312                 return 0;
2313         else
2314                 timeout = ieee->current_network.beacon_interval;
2315
2316         //printk("VALID\n");
2317         ieee->current_network.dtim_data = IEEE80211_DTIM_INVALID;
2318
2319         if(dtim & ((IEEE80211_DTIM_UCAST | IEEE80211_DTIM_MBCAST)& ieee->ps))
2320                 return 2;
2321
2322         if(!time_after(jiffies, ieee->dev->trans_start + MSECS(timeout)))
2323                 return 0;
2324
2325         if(!time_after(jiffies, ieee->last_rx_ps_time + MSECS(timeout)))
2326                 return 0;
2327
2328         if((ieee->softmac_features & IEEE_SOFTMAC_SINGLE_QUEUE ) &&
2329                 (ieee->mgmt_queue_tail != ieee->mgmt_queue_head))
2330                 return 0;
2331 #if 0
2332         if(time_l){
2333                 *time_l = ieee->current_network.last_dtim_sta_time[0]
2334                         + (ieee->current_network.beacon_interval
2335                         * ieee->current_network.dtim_period) * 1000;
2336         }
2337 #else
2338         if(time_l){
2339                 *time_l = ieee->current_network.last_dtim_sta_time[0]
2340                         + MSECS((ieee->current_network.beacon_interval));
2341                         //* ieee->current_network.dtim_period));
2342                         //printk("beacon_interval:%x, dtim_period:%x, totol to Msecs:%x, HZ:%x\n", ieee->current_network.beacon_interval, ieee->current_network.dtim_period, MSECS(((ieee->current_network.beacon_interval * ieee->current_network.dtim_period))), HZ);
2343         }
2344
2345 #endif
2346         if(time_h){
2347                 *time_h = ieee->current_network.last_dtim_sta_time[1];
2348                 if(time_l && *time_l < ieee->current_network.last_dtim_sta_time[0])
2349                         *time_h += 1;
2350         }
2351
2352         return 1;
2353
2354
2355 }
2356
2357 inline void ieee80211_sta_ps(struct ieee80211_device *ieee)
2358 {
2359
2360         u32 th,tl;
2361         short sleep;
2362
2363         unsigned long flags,flags2;
2364
2365         spin_lock_irqsave(&ieee->lock, flags);
2366
2367         if((ieee->ps == IEEE80211_PS_DISABLED ||
2368
2369                 ieee->iw_mode != IW_MODE_INFRA ||
2370                 ieee->state != IEEE80211_LINKED)){
2371
2372                 //#warning CHECK_LOCK_HERE
2373                 spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
2374
2375                 ieee80211_sta_wakeup(ieee, 1);
2376
2377                 spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
2378         }
2379
2380         sleep = ieee80211_sta_ps_sleep(ieee,&th, &tl);
2381 //      printk("===>%s,%d[2 wake, 1 sleep, 0 do nothing], ieee->sta_sleep = %d\n",__func__, sleep,ieee->sta_sleep);
2382         /* 2 wake, 1 sleep, 0 do nothing */
2383         if(sleep == 0)
2384                 goto out;
2385
2386         if(sleep == 1){
2387
2388                 if(ieee->sta_sleep == 1)
2389                         ieee->enter_sleep_state(ieee->dev,th,tl);
2390
2391                 else if(ieee->sta_sleep == 0){
2392         //              printk("send null 1\n");
2393                         spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
2394
2395                         if(ieee->ps_is_queue_empty(ieee->dev)){
2396
2397
2398                                 ieee->sta_sleep = 2;
2399
2400                                 ieee->ps_request_tx_ack(ieee->dev);
2401
2402                                 ieee80211_sta_ps_send_null_frame(ieee,1);
2403
2404                                 ieee->ps_th = th;
2405                                 ieee->ps_tl = tl;
2406                         }
2407                         spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
2408
2409                 }
2410
2411
2412         }else if(sleep == 2){
2413 //#warning CHECK_LOCK_HERE
2414                 spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
2415
2416         //      printk("send wakeup packet\n");
2417                 ieee80211_sta_wakeup(ieee,1);
2418
2419                 spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
2420         }
2421
2422 out:
2423         spin_unlock_irqrestore(&ieee->lock, flags);
2424
2425 }
2426
2427 void ieee80211_sta_wakeup(struct ieee80211_device *ieee, short nl)
2428 {
2429         if(ieee->sta_sleep == 0){
2430                 if(nl){
2431                 //      printk("Warning: driver is probably failing to report TX ps error\n");
2432                         ieee->ps_request_tx_ack(ieee->dev);
2433                         ieee80211_sta_ps_send_null_frame(ieee, 0);
2434                 }
2435                 return;
2436
2437         }
2438
2439         if(ieee->sta_sleep == 1)
2440                 ieee->sta_wake_up(ieee->dev);
2441
2442         ieee->sta_sleep = 0;
2443
2444         if(nl){
2445                 ieee->ps_request_tx_ack(ieee->dev);
2446                 ieee80211_sta_ps_send_null_frame(ieee, 0);
2447         }
2448 }
2449
2450 void ieee80211_ps_tx_ack(struct ieee80211_device *ieee, short success)
2451 {
2452         unsigned long flags,flags2;
2453
2454         spin_lock_irqsave(&ieee->lock, flags);
2455         if(ieee->sta_sleep == 2){
2456                 /* Null frame with PS bit set */
2457                 if(success){
2458
2459                 //      printk("==================> %s::enter sleep state\n",__func__);
2460                         ieee->sta_sleep = 1;
2461                         ieee->enter_sleep_state(ieee->dev,ieee->ps_th,ieee->ps_tl);
2462                 }
2463                 /* if the card report not success we can't be sure the AP
2464                  * has not RXed so we can't assume the AP believe us awake
2465                  */
2466         }
2467         /* 21112005 - tx again null without PS bit if lost */
2468         else {
2469
2470                 if((ieee->sta_sleep == 0) && !success){
2471                         spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
2472                         ieee80211_sta_ps_send_null_frame(ieee, 0);
2473                         spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
2474                 }
2475         }
2476         spin_unlock_irqrestore(&ieee->lock, flags);
2477 }
2478
2479 inline int
2480 ieee80211_rx_frame_softmac(struct ieee80211_device *ieee, struct sk_buff *skb,
2481                         struct ieee80211_rx_stats *rx_stats, u16 type,
2482                         u16 stype)
2483 {
2484         struct ieee80211_hdr_3addr *header = (struct ieee80211_hdr_3addr *) skb->data;
2485         u16 errcode;
2486         u8* challenge=NULL;
2487         int chlen=0;
2488         int aid=0;
2489         struct ieee80211_assoc_response_frame *assoc_resp;
2490         struct ieee80211_info_element *info_element;
2491
2492         if(!ieee->proto_started)
2493                 return 0;
2494
2495         if(ieee->sta_sleep || (ieee->ps != IEEE80211_PS_DISABLED &&
2496                 ieee->iw_mode == IW_MODE_INFRA &&
2497                 ieee->state == IEEE80211_LINKED))
2498
2499                 tasklet_schedule(&ieee->ps_task);
2500
2501         if(WLAN_FC_GET_STYPE(header->frame_ctl) != IEEE80211_STYPE_PROBE_RESP &&
2502                 WLAN_FC_GET_STYPE(header->frame_ctl) != IEEE80211_STYPE_BEACON)
2503                 ieee->last_rx_ps_time = jiffies;
2504
2505         switch (WLAN_FC_GET_STYPE(header->frame_ctl)) {
2506
2507                 case IEEE80211_STYPE_ASSOC_RESP:
2508                 case IEEE80211_STYPE_REASSOC_RESP:
2509
2510                         IEEE80211_DEBUG_MGMT("received [RE]ASSOCIATION RESPONSE (%d)\n",
2511                                         WLAN_FC_GET_STYPE(header->frame_ctl));
2512                         if ((ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) &&
2513                                 ieee->state == IEEE80211_ASSOCIATING_AUTHENTICATED &&
2514                                 ieee->iw_mode == IW_MODE_INFRA){
2515                                 if (0 == (errcode=assoc_parse(skb, &aid))){
2516                                         u16 left;
2517
2518                                         ieee->state=IEEE80211_LINKED;
2519                                         ieee->assoc_id = aid;
2520                                         ieee->softmac_stats.rx_ass_ok++;
2521
2522                                         //printk(KERN_WARNING "nic_type = %s", (rx_stats->nic_type == 1)?"rtl8187":"rtl8187B");
2523                                         if(1 == rx_stats->nic_type) //card type is 8187
2524                                         {
2525                                                 goto associate_complete;
2526                                         }
2527                                         assoc_resp = (struct ieee80211_assoc_response_frame*)skb->data;
2528                                         info_element =  &assoc_resp->info_element;
2529                                         left = skb->len - ((void*)info_element - (void*)assoc_resp);
2530
2531                                         while (left >= sizeof(struct ieee80211_info_element_hdr)) {
2532                                                 if (sizeof(struct ieee80211_info_element_hdr) + info_element->len > left) {
2533                                                         printk(KERN_WARNING "[re]associate reeponse error!");
2534                                                         return 1;
2535                                                 }
2536                                                 switch (info_element->id) {
2537                                                   case MFIE_TYPE_GENERIC:
2538                                                          IEEE80211_DEBUG_SCAN("MFIE_TYPE_GENERIC: %d bytes\n", info_element->len);
2539                                                         if (info_element->len >= 8  &&
2540                                                             info_element->data[0] == 0x00 &&
2541                                                             info_element->data[1] == 0x50 &&
2542                                                             info_element->data[2] == 0xf2 &&
2543                                                             info_element->data[3] == 0x02 &&
2544                                                             info_element->data[4] == 0x01) {
2545                                                             // Not care about version at present.
2546                                                             //WMM Parameter Element
2547                                                             memcpy(ieee->current_network.wmm_param,(u8*)(info_element->data\
2548                                                                                     + 8),(info_element->len - 8));
2549
2550                                                             if (((ieee->current_network.wmm_info^info_element->data[6])& \
2551                                                                                     0x0f)||(!ieee->init_wmmparam_flag)) {
2552                                                               //refresh paramete element for current network
2553                                                               // update the register parameter for hardware
2554                                                               ieee->init_wmmparam_flag = 1;
2555                                                               queue_work(ieee->wq, &ieee->wmm_param_update_wq);
2556
2557                                                             }
2558                                                             //update info_element for current network
2559                                                             ieee->current_network.wmm_info  = info_element->data[6];
2560                                                         }
2561                                                         break;
2562                                                   default:
2563                                                         //nothing to do at present!!!
2564                                                         break;
2565                                                 }
2566
2567                                                 left -= sizeof(struct ieee80211_info_element_hdr) +
2568                                                         info_element->len;
2569                                                 info_element = (struct ieee80211_info_element *)
2570                                                         &info_element->data[info_element->len];
2571                                         }
2572                                         if(!ieee->init_wmmparam_flag) //legacy AP, reset the AC_xx_param register
2573                                         {
2574                                                 queue_work(ieee->wq,&ieee->wmm_param_update_wq);
2575                                                 ieee->init_wmmparam_flag = 1;//indicate AC_xx_param upated since last associate
2576                                         }
2577 associate_complete:
2578                                         ieee80211_associate_complete(ieee);
2579                                 }else{
2580                                         ieee->softmac_stats.rx_ass_err++;
2581                                         IEEE80211_DEBUG_MGMT(
2582                                                 "Association response status code 0x%x\n",
2583                                                 errcode);
2584                                         ieee80211_associate_abort(ieee);
2585                                 }
2586                         }
2587 #ifdef _RTL8187_EXT_PATCH_
2588                         else if ((ieee->iw_mode == ieee->iw_ext_mode) && ieee->ext_patch_ieee80211_rx_frame_softmac_on_assoc_rsp)
2589                         {
2590                                         ieee->ext_patch_ieee80211_rx_frame_softmac_on_assoc_rsp(ieee, skb);
2591                         }
2592 #endif
2593                         break;
2594
2595                 case IEEE80211_STYPE_ASSOC_REQ:
2596                 case IEEE80211_STYPE_REASSOC_REQ:
2597
2598                         if ((ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) &&
2599                                 ieee->iw_mode == IW_MODE_MASTER)
2600
2601                                 ieee80211_rx_assoc_rq(ieee, skb);
2602 #ifdef _RTL8187_EXT_PATCH_
2603                         else if ((ieee->iw_mode == ieee->iw_ext_mode) && ieee->ext_patch_ieee80211_rx_frame_softmac_on_assoc_req)
2604                         {
2605                                         ieee->ext_patch_ieee80211_rx_frame_softmac_on_assoc_req(ieee, skb);
2606                         }
2607 #endif
2608                         break;
2609
2610                 case IEEE80211_STYPE_AUTH:
2611
2612 #ifdef _RTL8187_EXT_PATCH_
2613 printk("IEEE80211_STYPE_AUTH\n");
2614                         if((ieee->iw_mode == ieee->iw_ext_mode) && ieee->ext_patch_ieee80211_rx_frame_softmac_on_auth)
2615                         if( ieee->ext_patch_ieee80211_rx_frame_softmac_on_auth(ieee, skb, rx_stats) );
2616 #endif
2617                         if (ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE){
2618                                 if (ieee->state == IEEE80211_ASSOCIATING_AUTHENTICATING &&
2619                                 ieee->iw_mode == IW_MODE_INFRA){
2620
2621                                                 IEEE80211_DEBUG_MGMT("Received authentication response");
2622
2623                                                 if (0 == (errcode=auth_parse(skb, &challenge, &chlen))){
2624                                                         if(ieee->open_wep || !challenge){
2625                                                                 ieee->state = IEEE80211_ASSOCIATING_AUTHENTICATED;
2626                                                                 ieee->softmac_stats.rx_auth_rs_ok++;
2627
2628                                                                 ieee80211_associate_step2(ieee);
2629                                                         }else{
2630                                                                 ieee80211_auth_challenge(ieee, challenge, chlen);
2631                                                         }
2632                                                 }else{
2633                                                         ieee->softmac_stats.rx_auth_rs_err++;
2634                                                         IEEE80211_DEBUG_MGMT("Authentication respose status code 0x%x",errcode);
2635                                                         ieee80211_associate_abort(ieee);
2636                                                 }
2637
2638                                         }else if (ieee->iw_mode == IW_MODE_MASTER){
2639                                                 ieee80211_rx_auth_rq(ieee, skb);
2640                                         }
2641                                 }
2642                         break;
2643
2644                 case IEEE80211_STYPE_PROBE_REQ:
2645
2646                         if ((ieee->softmac_features & IEEE_SOFTMAC_PROBERS) &&
2647                                 ((ieee->iw_mode == IW_MODE_ADHOC ||
2648                                 ieee->iw_mode == IW_MODE_MASTER) &&
2649                                 ieee->state == IEEE80211_LINKED))
2650
2651                                 ieee80211_rx_probe_rq(ieee, skb);
2652                         break;
2653
2654                 case IEEE80211_STYPE_DISASSOC:
2655                 case IEEE80211_STYPE_DEAUTH:
2656 #ifdef _RTL8187_EXT_PATCH_
2657 printk("IEEE80211_STYPE_DEAUTH\n");
2658                 if((ieee->iw_mode == ieee->iw_ext_mode) && ieee->ext_patch_ieee80211_rx_frame_softmac_on_deauth)
2659                 if( ieee->ext_patch_ieee80211_rx_frame_softmac_on_deauth(ieee, skb, rx_stats) ) ;
2660 #endif
2661                         /* FIXME for now repeat all the association procedure
2662                         * both for disassociation and deauthentication
2663                         */
2664                         if ((ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) &&
2665                                 (ieee->state == IEEE80211_LINKED) &&
2666                                 (ieee->iw_mode == IW_MODE_INFRA) &&
2667                                 (!memcmp(header->addr2,ieee->current_network.bssid,ETH_ALEN))){
2668                                 ieee->state = IEEE80211_ASSOCIATING;
2669                                 ieee->softmac_stats.reassoc++;
2670
2671                                 //notify_wx_assoc_event(ieee);  //YJ,del,080828, do not notify os here
2672                                 queue_work(ieee->wq, &ieee->associate_procedure_wq);
2673                         }
2674
2675                         break;
2676
2677                 default:
2678                         return -1;
2679                         break;
2680         }
2681
2682         //dev_kfree_skb_any(skb);
2683         return 0;
2684 }
2685
2686
2687
2688 /* following are for a simplier TX queue management.
2689  * Instead of using netif_[stop/wake]_queue the driver
2690  * will uses these two function (plus a reset one), that
2691  * will internally uses the kernel netif_* and takes
2692  * care of the ieee802.11 fragmentation.
2693  * So the driver receives a fragment per time and might
2694  * call the stop function when it want without take care
2695  * to have enought room to TX an entire packet.
2696  * This might be useful if each fragment need it's own
2697  * descriptor, thus just keep a total free memory > than
2698  * the max fragmentation treshold is not enought.. If the
2699  * ieee802.11 stack passed a TXB struct then you needed
2700  * to keep N free descriptors where
2701  * N = MAX_PACKET_SIZE / MIN_FRAG_TRESHOLD
2702  * In this way you need just one and the 802.11 stack
2703  * will take care of buffering fragments and pass them to
2704  * to the driver later, when it wakes the queue.
2705  */
2706
2707 void ieee80211_softmac_xmit(struct ieee80211_txb *txb, struct ieee80211_device *ieee)
2708 {
2709
2710
2711         unsigned long flags;
2712         int  i;
2713 #ifdef _RTL8187_EXT_PATCH_
2714         int rate = ieee->rate;
2715 #endif
2716
2717         spin_lock_irqsave(&ieee->lock,flags);
2718         #if 0
2719         if(ieee->queue_stop){
2720                 IEEE80211DMESG("EE: IEEE hard_start_xmit invoked when kernel queue should be stopped");
2721                 netif_stop_queue(ieee->dev);
2722                 ieee->ieee_stats.swtxstop++;
2723                 //dev_kfree_skb_any(skb);
2724                 err = 1;
2725                 goto exit;
2726         }
2727
2728         ieee->stats.tx_bytes+=skb->len;
2729
2730
2731         txb=ieee80211_skb_to_txb(ieee,skb);
2732
2733
2734         if(txb==NULL){
2735                 IEEE80211DMESG("WW: IEEE stack failed to provide txb");
2736                 //dev_kfree_skb_any(skb);
2737                 err = 1;
2738                 goto exit;
2739         }
2740         #endif
2741
2742 #ifdef _RTL8187_EXT_PATCH_
2743         if((ieee->iw_mode == ieee->iw_ext_mode) && ieee->ext_patch_ieee80211_softmac_xmit_get_rate && txb->nr_frags)
2744         {
2745                 rate = ieee->ext_patch_ieee80211_softmac_xmit_get_rate(ieee, txb->fragments[0]);
2746         }
2747 #endif
2748         /* called with 2nd parm 0, no tx mgmt lock required */
2749         ieee80211_sta_wakeup(ieee,0);
2750
2751         for(i = 0; i < txb->nr_frags; i++) {
2752
2753                 if (ieee->queue_stop){
2754                         ieee->tx_pending.txb = txb;
2755                         ieee->tx_pending.frag = i;
2756                         goto exit;
2757                 }else{
2758                         ieee->softmac_data_hard_start_xmit(
2759                                 txb->fragments[i],
2760 #ifdef _RTL8187_EXT_PATCH_
2761                                 ieee->dev, rate);
2762 #else
2763                                 ieee->dev,ieee->rate);
2764 #endif
2765                                 //(i+1)<txb->nr_frags);
2766                         ieee->stats.tx_packets++;
2767                         ieee->stats.tx_bytes += txb->fragments[i]->len;
2768                         ieee->dev->trans_start = jiffies;
2769                 }
2770         }
2771
2772         ieee80211_txb_free(txb);
2773
2774         exit:
2775         spin_unlock_irqrestore(&ieee->lock,flags);
2776
2777 }
2778
2779 /* called with ieee->lock acquired */
2780 void ieee80211_resume_tx(struct ieee80211_device *ieee)
2781 {
2782         int i;
2783         for(i = ieee->tx_pending.frag; i < ieee->tx_pending.txb->nr_frags; i++) {
2784
2785                 if (ieee->queue_stop){
2786                         ieee->tx_pending.frag = i;
2787                         return;
2788                 }else{
2789
2790                         ieee->softmac_data_hard_start_xmit(
2791                                 ieee->tx_pending.txb->fragments[i],
2792                                 ieee->dev,ieee->rate);
2793                                 //(i+1)<ieee->tx_pending.txb->nr_frags);
2794                         ieee->stats.tx_packets++;
2795                         ieee->dev->trans_start = jiffies;
2796                 }
2797         }
2798
2799
2800         ieee80211_txb_free(ieee->tx_pending.txb);
2801         ieee->tx_pending.txb = NULL;
2802 }
2803
2804
2805 void ieee80211_reset_queue(struct ieee80211_device *ieee)
2806 {
2807         unsigned long flags;
2808
2809         spin_lock_irqsave(&ieee->lock,flags);
2810         init_mgmt_queue(ieee);
2811         if (ieee->tx_pending.txb){
2812                 ieee80211_txb_free(ieee->tx_pending.txb);
2813                 ieee->tx_pending.txb = NULL;
2814         }
2815         ieee->queue_stop = 0;
2816         spin_unlock_irqrestore(&ieee->lock,flags);
2817
2818 }
2819
2820 void ieee80211_wake_queue(struct ieee80211_device *ieee)
2821 {
2822
2823         unsigned long flags;
2824         struct sk_buff *skb;
2825         struct ieee80211_hdr_3addr  *header;
2826
2827         spin_lock_irqsave(&ieee->lock,flags);
2828         if (! ieee->queue_stop) goto exit;
2829
2830         ieee->queue_stop = 0;
2831
2832         if(ieee->softmac_features & IEEE_SOFTMAC_SINGLE_QUEUE){
2833                 while (!ieee->queue_stop && (skb = dequeue_mgmt(ieee))){
2834
2835                         header = (struct ieee80211_hdr_3addr  *) skb->data;
2836
2837                         header->seq_ctl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
2838
2839                         if (ieee->seq_ctrl[0] == 0xFFF)
2840                                 ieee->seq_ctrl[0] = 0;
2841                         else
2842                                 ieee->seq_ctrl[0]++;
2843
2844                         //printk(KERN_ALERT "ieee80211_wake_queue \n");
2845                         ieee->softmac_data_hard_start_xmit(skb,ieee->dev,ieee->basic_rate);
2846                         dev_kfree_skb_any(skb);//edit by thomas
2847                 }
2848         }
2849         if (!ieee->queue_stop && ieee->tx_pending.txb)
2850                 ieee80211_resume_tx(ieee);
2851
2852         if (!ieee->queue_stop && netif_queue_stopped(ieee->dev)){
2853                 ieee->softmac_stats.swtxawake++;
2854                 netif_wake_queue(ieee->dev);
2855         }
2856
2857 exit :
2858         spin_unlock_irqrestore(&ieee->lock,flags);
2859 }
2860
2861
2862 void ieee80211_stop_queue(struct ieee80211_device *ieee)
2863 {
2864         //unsigned long flags;
2865         //spin_lock_irqsave(&ieee->lock,flags);
2866
2867         if (! netif_queue_stopped(ieee->dev)){
2868                 netif_stop_queue(ieee->dev);
2869                 ieee->softmac_stats.swtxstop++;
2870         }
2871         ieee->queue_stop = 1;
2872         //spin_unlock_irqrestore(&ieee->lock,flags);
2873
2874 }
2875
2876
2877 inline void ieee80211_randomize_cell(struct ieee80211_device *ieee)
2878 {
2879
2880         get_random_bytes(ieee->current_network.bssid, ETH_ALEN);
2881
2882         /* an IBSS cell address must have the two less significant
2883          * bits of the first byte = 2
2884          */
2885         ieee->current_network.bssid[0] &= ~0x01;
2886         ieee->current_network.bssid[0] |= 0x02;
2887 }
2888
2889 /* called in user context only */
2890 void ieee80211_start_master_bss(struct ieee80211_device *ieee)
2891 {
2892         ieee->assoc_id = 1;
2893
2894         if (ieee->current_network.ssid_len == 0){
2895                 strncpy(ieee->current_network.ssid,
2896                         IEEE80211_DEFAULT_TX_ESSID,
2897                         IW_ESSID_MAX_SIZE);
2898
2899                 ieee->current_network.ssid_len = strlen(IEEE80211_DEFAULT_TX_ESSID);
2900                 ieee->ssid_set = 1;
2901         }
2902
2903         memcpy(ieee->current_network.bssid, ieee->dev->dev_addr, ETH_ALEN);
2904
2905         ieee->set_chan(ieee->dev, ieee->current_network.channel);
2906         ieee->state = IEEE80211_LINKED;
2907         ieee->link_change(ieee->dev);
2908         notify_wx_assoc_event(ieee);
2909
2910         if (ieee->data_hard_resume)
2911                 ieee->data_hard_resume(ieee->dev);
2912
2913         netif_carrier_on(ieee->dev);
2914 }
2915
2916 void ieee80211_start_monitor_mode(struct ieee80211_device *ieee)
2917 {
2918         if(ieee->raw_tx){
2919
2920                 if (ieee->data_hard_resume)
2921                         ieee->data_hard_resume(ieee->dev);
2922
2923                 netif_carrier_on(ieee->dev);
2924         }
2925 }
2926
2927 void ieee80211_start_ibss_wq(struct work_struct *work)
2928 {
2929         struct delayed_work *dwork = to_delayed_work(work);
2930         struct ieee80211_device *ieee = container_of(dwork, struct ieee80211_device, start_ibss_wq);
2931
2932         /* iwconfig mode ad-hoc will schedule this and return
2933          * on the other hand this will block further iwconfig SET
2934          * operations because of the wx_sem hold.
2935          * Anyway some most set operations set a flag to speed-up
2936          * (abort) this wq (when syncro scanning) before sleeping
2937          * on the semaphore
2938          */
2939
2940         down(&ieee->wx_sem);
2941
2942
2943         if (ieee->current_network.ssid_len == 0){
2944                 strcpy(ieee->current_network.ssid,IEEE80211_DEFAULT_TX_ESSID);
2945                 ieee->current_network.ssid_len = strlen(IEEE80211_DEFAULT_TX_ESSID);
2946                 ieee->ssid_set = 1;
2947         }
2948
2949         /* check if we have this cell in our network list */
2950         ieee80211_softmac_check_all_nets(ieee);
2951
2952 #ifdef ENABLE_DOT11D
2953         if(ieee->state == IEEE80211_NOLINK)
2954                 ieee->current_network.channel = 10;
2955 #endif
2956         /* if not then the state is not linked. Maybe the user swithced to
2957          * ad-hoc mode just after being in monitor mode, or just after
2958          * being very few time in managed mode (so the card have had no
2959          * time to scan all the chans..) or we have just run up the iface
2960          * after setting ad-hoc mode. So we have to give another try..
2961          * Here, in ibss mode, should be safe to do this without extra care
2962          * (in bss mode we had to make sure no-one tryed to associate when
2963          * we had just checked the ieee->state and we was going to start the
2964          * scan) beacause in ibss mode the ieee80211_new_net function, when
2965          * finds a good net, just set the ieee->state to IEEE80211_LINKED,
2966          * so, at worst, we waste a bit of time to initiate an unneeded syncro
2967          * scan, that will stop at the first round because it sees the state
2968          * associated.
2969          */
2970         if (ieee->state == IEEE80211_NOLINK)
2971                 ieee80211_start_scan_syncro(ieee);
2972
2973         /* the network definitively is not here.. create a new cell */
2974         if (ieee->state == IEEE80211_NOLINK){
2975                 printk("creating new IBSS cell\n");
2976                 if(!ieee->wap_set)
2977                         ieee80211_randomize_cell(ieee);
2978
2979                 if(ieee->modulation & IEEE80211_CCK_MODULATION){
2980
2981                         ieee->current_network.rates_len = 4;
2982
2983                         ieee->current_network.rates[0] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_1MB;
2984                         ieee->current_network.rates[1] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_2MB;
2985                         ieee->current_network.rates[2] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_5MB;
2986                         ieee->current_network.rates[3] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_11MB;
2987
2988                 }else
2989                         ieee->current_network.rates_len = 0;
2990
2991                 if(ieee->modulation & IEEE80211_OFDM_MODULATION){
2992                         ieee->current_network.rates_ex_len = 8;
2993
2994                         ieee->current_network.rates_ex[0] = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_6MB;
2995                         ieee->current_network.rates_ex[1] = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_9MB;
2996                         ieee->current_network.rates_ex[2] = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_12MB;
2997                         ieee->current_network.rates_ex[3] = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_18MB;
2998                         ieee->current_network.rates_ex[4] = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_24MB;
2999                         ieee->current_network.rates_ex[5] = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_36MB;
3000                         ieee->current_network.rates_ex[6] = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_48MB;
3001                         ieee->current_network.rates_ex[7] = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_54MB;
3002
3003                         ieee->rate = 540;
3004                 }else{
3005                         ieee->current_network.rates_ex_len = 0;
3006                         ieee->rate = 110;
3007                 }
3008
3009                 // By default, WMM function will be disabled in IBSS mode
3010                 ieee->current_network.QoS_Enable = 0;
3011
3012                 ieee->current_network.atim_window = 0;
3013                 ieee->current_network.capability = WLAN_CAPABILITY_IBSS;
3014                 if(ieee->short_slot)
3015                         ieee->current_network.capability |= WLAN_CAPABILITY_SHORT_SLOT;
3016
3017         }
3018
3019         ieee->state = IEEE80211_LINKED;
3020         ieee->set_chan(ieee->dev, ieee->current_network.channel);
3021         ieee->link_change(ieee->dev);
3022
3023         notify_wx_assoc_event(ieee);
3024
3025         ieee80211_start_send_beacons(ieee);
3026         printk(KERN_WARNING "after sending beacon packet!\n");
3027
3028         if (ieee->data_hard_resume)
3029                 ieee->data_hard_resume(ieee->dev);
3030
3031         netif_carrier_on(ieee->dev);
3032
3033         up(&ieee->wx_sem);
3034 }
3035 inline void ieee80211_start_ibss(struct ieee80211_device *ieee)
3036 {
3037         queue_delayed_work(ieee->wq, &ieee->start_ibss_wq, 100);
3038 }
3039
3040 /* this is called only in user context, with wx_sem held */
3041 void ieee80211_start_bss(struct ieee80211_device *ieee)
3042 {
3043         unsigned long flags;
3044 #ifdef ENABLE_DOT11D
3045         //
3046         // Ref: 802.11d 11.1.3.3
3047         // STA shall not start a BSS unless properly formed Beacon frame including a Country IE.
3048         //
3049         if(IS_DOT11D_ENABLE(ieee) && !IS_COUNTRY_IE_VALID(ieee))
3050         {
3051                 if(! ieee->bGlobalDomain)
3052                 {
3053                         return;
3054                 }
3055         }
3056 #endif
3057         /* check if we have already found the net we
3058          * are interested in (if any).
3059          * if not (we are disassociated and we are not
3060          * in associating / authenticating phase) start the background scanning.
3061          */
3062         ieee80211_softmac_check_all_nets(ieee);
3063
3064         /* ensure no-one start an associating process (thus setting
3065          * the ieee->state to ieee80211_ASSOCIATING) while we
3066          * have just cheked it and we are going to enable scan.
3067          * The ieee80211_new_net function is always called with
3068          * lock held (from both ieee80211_softmac_check_all_nets and
3069          * the rx path), so we cannot be in the middle of such function
3070          */
3071         spin_lock_irqsave(&ieee->lock, flags);
3072
3073 //#ifdef ENABLE_IPS
3074 //      printk("start bss ENABLE_IPS\n");
3075 //#else
3076         if (ieee->state == IEEE80211_NOLINK){
3077                 ieee->actscanning = true;
3078                 ieee80211_start_scan(ieee);
3079         }
3080 //#endif
3081         spin_unlock_irqrestore(&ieee->lock, flags);
3082 }
3083
3084 /* called only in userspace context */
3085 void ieee80211_disassociate(struct ieee80211_device *ieee)
3086 {
3087         netif_carrier_off(ieee->dev);
3088
3089         if (ieee->softmac_features & IEEE_SOFTMAC_TX_QUEUE)
3090                         ieee80211_reset_queue(ieee);
3091
3092         if (ieee->data_hard_stop)
3093                         ieee->data_hard_stop(ieee->dev);
3094
3095 #ifdef ENABLE_DOT11D
3096         if(IS_DOT11D_ENABLE(ieee))
3097                 Dot11d_Reset(ieee);
3098 #endif
3099         ieee->state = IEEE80211_NOLINK;
3100         ieee->link_change(ieee->dev);
3101         notify_wx_assoc_event(ieee);
3102
3103 }
3104 void ieee80211_associate_retry_wq(struct work_struct *work)
3105 {
3106         struct delayed_work *dwork = to_delayed_work(work);
3107         struct ieee80211_device *ieee = container_of(dwork, struct ieee80211_device, associate_retry_wq);
3108         unsigned long flags;
3109         down(&ieee->wx_sem);
3110         if(!ieee->proto_started)
3111                 goto exit;
3112         if(ieee->state != IEEE80211_ASSOCIATING_RETRY)
3113                 goto exit;
3114         /* until we do not set the state to IEEE80211_NOLINK
3115         * there are no possibility to have someone else trying
3116         * to start an association procdure (we get here with
3117         * ieee->state = IEEE80211_ASSOCIATING).
3118         * When we set the state to IEEE80211_NOLINK it is possible
3119         * that the RX path run an attempt to associate, but
3120         * both ieee80211_softmac_check_all_nets and the
3121         * RX path works with ieee->lock held so there are no
3122         * problems. If we are still disassociated then start a scan.
3123         * the lock here is necessary to ensure no one try to start
3124         * an association procedure when we have just checked the
3125         * state and we are going to start the scan.
3126         */
3127         ieee->state = IEEE80211_NOLINK;
3128         ieee->beinretry = true;
3129         ieee80211_softmac_check_all_nets(ieee);
3130
3131         spin_lock_irqsave(&ieee->lock, flags);
3132
3133         if(ieee->state == IEEE80211_NOLINK){
3134                 ieee->beinretry = false;
3135                 ieee->actscanning = true;
3136                 ieee80211_start_scan(ieee);
3137         }
3138         //YJ,add,080828, notify os here
3139         if(ieee->state == IEEE80211_NOLINK)
3140         {
3141                 notify_wx_assoc_event(ieee);
3142         }
3143         //YJ,add,080828,end
3144         spin_unlock_irqrestore(&ieee->lock, flags);
3145
3146 exit:
3147         up(&ieee->wx_sem);
3148 }
3149
3150 struct sk_buff *ieee80211_get_beacon_(struct ieee80211_device *ieee)
3151 {
3152         u8 broadcast_addr[] = {0xff,0xff,0xff,0xff,0xff,0xff};
3153
3154         struct sk_buff *skb = NULL;
3155         struct ieee80211_probe_response *b;
3156
3157 //rz
3158 #ifdef _RTL8187_EXT_PATCH_
3159         if((ieee->iw_mode == ieee->iw_ext_mode) && ieee->ext_patch_get_beacon_get_probersp )
3160                 skb = ieee->ext_patch_get_beacon_get_probersp(ieee, broadcast_addr, &(ieee->current_network));
3161         else
3162                 skb = ieee80211_probe_resp(ieee, broadcast_addr);
3163 #else
3164         skb = ieee80211_probe_resp(ieee, broadcast_addr);
3165 #endif
3166 //
3167         if (!skb)
3168                 return NULL;
3169
3170         b = (struct ieee80211_probe_response *) skb->data;
3171         b->header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_BEACON);
3172
3173         return skb;
3174
3175 }
3176
3177 struct sk_buff *ieee80211_get_beacon(struct ieee80211_device *ieee)
3178 {
3179         struct sk_buff *skb;
3180         struct ieee80211_probe_response *b;
3181
3182         skb = ieee80211_get_beacon_(ieee);
3183         if(!skb)
3184                 return NULL;
3185
3186         b = (struct ieee80211_probe_response *) skb->data;
3187         b->header.seq_ctrl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
3188
3189         if (ieee->seq_ctrl[0] == 0xFFF)
3190                 ieee->seq_ctrl[0] = 0;
3191         else
3192                 ieee->seq_ctrl[0]++;
3193
3194         return skb;
3195 }
3196
3197 void ieee80211_softmac_stop_protocol(struct ieee80211_device *ieee)
3198 {
3199         ieee->sync_scan_hurryup = 1;
3200         down(&ieee->wx_sem);
3201         ieee80211_stop_protocol(ieee);
3202         up(&ieee->wx_sem);
3203 }
3204
3205
3206 void ieee80211_stop_protocol(struct ieee80211_device *ieee)
3207 {
3208         if (!ieee->proto_started)
3209                 return;
3210
3211         ieee->proto_started = 0;
3212
3213 #ifdef _RTL8187_EXT_PATCH_
3214         if(ieee->ext_patch_ieee80211_stop_protocol)
3215                 ieee->ext_patch_ieee80211_stop_protocol(ieee);
3216 //if call queue_delayed_work,can call this,or do nothing..
3217 //edit by lawrence,20071118
3218 //      cancel_delayed_work(&ieee->ext_stop_scan_wq);
3219 //      cancel_delayed_work(&ieee->ext_send_beacon_wq);
3220 #endif // _RTL8187_EXT_PATCH_
3221
3222         ieee80211_stop_send_beacons(ieee);
3223         if((ieee->iw_mode == IW_MODE_INFRA)&&(ieee->state == IEEE80211_LINKED)) {
3224                 SendDisassociation(ieee,NULL,WLAN_REASON_DISASSOC_STA_HAS_LEFT);
3225         }
3226         del_timer_sync(&ieee->associate_timer);
3227         cancel_delayed_work(&ieee->associate_retry_wq);
3228         cancel_delayed_work(&ieee->start_ibss_wq);
3229         ieee80211_stop_scan(ieee);
3230
3231         ieee80211_disassociate(ieee);
3232 }
3233
3234 void ieee80211_softmac_start_protocol(struct ieee80211_device *ieee)
3235 {
3236         ieee->sync_scan_hurryup = 0;
3237         down(&ieee->wx_sem);
3238         ieee80211_start_protocol(ieee);
3239         up(&ieee->wx_sem);
3240 }
3241
3242 void ieee80211_start_protocol(struct ieee80211_device *ieee)
3243 {
3244         short ch = 0;
3245         int i = 0;
3246
3247         if (ieee->proto_started)
3248                 return;
3249
3250         ieee->proto_started = 1;
3251
3252         if (ieee->current_network.channel == 0){
3253                 do{
3254                         ch++;
3255                         if (ch > MAX_CHANNEL_NUMBER)
3256                                 return; /* no channel found */
3257
3258 #ifdef ENABLE_DOT11D
3259                 }while(!GET_DOT11D_INFO(ieee)->channel_map[ch]);
3260 #else
3261                 }while(!ieee->channel_map[ch]);
3262 #endif
3263
3264                 ieee->current_network.channel = ch;
3265         }
3266
3267         if (ieee->current_network.beacon_interval == 0)
3268                 ieee->current_network.beacon_interval = 100;
3269         ieee->set_chan(ieee->dev,ieee->current_network.channel);
3270
3271         for(i = 0; i < 17; i++) {
3272           ieee->last_rxseq_num[i] = -1;
3273           ieee->last_rxfrag_num[i] = -1;
3274           ieee->last_packet_time[i] = 0;
3275         }
3276
3277         ieee->init_wmmparam_flag = 0;//reinitialize AC_xx_PARAM registers.
3278
3279
3280         /* if the user set the MAC of the ad-hoc cell and then
3281          * switch to managed mode, shall we  make sure that association
3282          * attempts does not fail just because the user provide the essid
3283          * and the nic is still checking for the AP MAC ??
3284          */
3285         switch (ieee->iw_mode) {
3286                 case IW_MODE_AUTO:
3287                         ieee->iw_mode = IW_MODE_INFRA;
3288                         //not set break here intentionly
3289                 case IW_MODE_INFRA:
3290                         ieee80211_start_bss(ieee);
3291                         break;
3292
3293                 case IW_MODE_ADHOC:
3294                         ieee80211_start_ibss(ieee);
3295                         break;
3296
3297                 case IW_MODE_MASTER:
3298                         ieee80211_start_master_bss(ieee);
3299                 break;
3300
3301                 case IW_MODE_MONITOR:
3302                         ieee80211_start_monitor_mode(ieee);
3303                         break;
3304
3305                 default:
3306 #ifdef _RTL8187_EXT_PATCH_
3307                         if((ieee->iw_mode == ieee->iw_ext_mode) &&\
3308                             ieee->ext_patch_ieee80211_start_protocol &&\
3309                             ieee->ext_patch_ieee80211_start_protocol(ieee)) {
3310                                 queue_work(ieee->wq, &ieee->ext_stop_scan_wq);
3311
3312                                 // By default, WMM function will be disabled in
3313                                 // EXTENSION mode
3314                                 ieee->current_network.QoS_Enable = 0;
3315
3316                                 if(ieee->modulation & IEEE80211_CCK_MODULATION){
3317                                         ieee->current_network.rates_len = 4;
3318                                         ieee->current_network.rates[0] = \
3319                                               IEEE80211_BASIC_RATE_MASK | \
3320                                               IEEE80211_CCK_RATE_1MB;
3321                                         ieee->current_network.rates[1] = \
3322                                               IEEE80211_BASIC_RATE_MASK |\
3323                                               IEEE80211_CCK_RATE_2MB;
3324                                         ieee->current_network.rates[2] = \
3325                                               IEEE80211_BASIC_RATE_MASK |\
3326                                               IEEE80211_CCK_RATE_5MB;
3327                                         ieee->current_network.rates[3] = \
3328                                               IEEE80211_BASIC_RATE_MASK |\
3329                                               IEEE80211_CCK_RATE_11MB;
3330                                 }else
3331                                         ieee->current_network.rates_len = 0;
3332
3333                                 if(ieee->modulation & IEEE80211_OFDM_MODULATION){
3334                                         ieee->current_network.rates_ex_len = 8;
3335                                         ieee->current_network.rates_ex[0] = \
3336                                               IEEE80211_BASIC_RATE_MASK |\
3337                                               IEEE80211_OFDM_RATE_6MB;
3338                                         ieee->current_network.rates_ex[1] = \
3339                                               IEEE80211_BASIC_RATE_MASK |\
3340                                               IEEE80211_OFDM_RATE_9MB;
3341                                         ieee->current_network.rates_ex[2] = \
3342                                               IEEE80211_BASIC_RATE_MASK |\
3343                                               IEEE80211_OFDM_RATE_12MB;
3344                                         ieee->current_network.rates_ex[3] = \
3345                                               IEEE80211_BASIC_RATE_MASK | \
3346                                               IEEE80211_OFDM_RATE_18MB;
3347                                         ieee->current_network.rates_ex[4] =\
3348                                               IEEE80211_BASIC_RATE_MASK |\
3349                                               IEEE80211_OFDM_RATE_24MB;
3350                                         ieee->current_network.rates_ex[5] =\
3351                                               IEEE80211_BASIC_RATE_MASK |\
3352                                               IEEE80211_OFDM_RATE_36MB;
3353                                         ieee->current_network.rates_ex[6] = \
3354                                               IEEE80211_BASIC_RATE_MASK |\
3355                                               IEEE80211_OFDM_RATE_48MB;
3356                                         ieee->current_network.rates_ex[7] =\
3357                                               IEEE80211_BASIC_RATE_MASK |\
3358                                               IEEE80211_OFDM_RATE_54MB;
3359                                         ieee->rate = 540;
3360                                 }else{
3361                                         ieee->current_network.rates_ex_len = 0;
3362                                         ieee->rate = 110;
3363                                 }
3364
3365                                 /*
3366                                    spin_lock_irqsave(&ieee->lock, flags);
3367                                    if (ieee->state == IEEE80211_NOLINK)
3368                                    ieee80211_start_scan(ieee);
3369                                 // ieee->set_chan(ieee->dev, 8);
3370
3371                                 spin_unlock_irqrestore(&ieee->lock, flags);
3372                                 */
3373                                 memcpy(ieee->current_network.bssid, ieee->dev->dev_addr,\
3374                                          ETH_ALEN);
3375                                 ieee->link_change(ieee->dev);
3376                                 notify_wx_assoc_event(ieee);
3377
3378                                 if (ieee->data_hard_resume)
3379                                         ieee->data_hard_resume(ieee->dev);
3380
3381                                 netif_carrier_on(ieee->dev);
3382                         } else {
3383                                 ieee->iw_mode = IW_MODE_INFRA;
3384                                 ieee80211_start_bss(ieee);
3385                         }
3386 #else
3387                         ieee->iw_mode = IW_MODE_INFRA;
3388                         ieee80211_start_bss(ieee);
3389
3390 #endif
3391                         break;
3392         }
3393 }
3394
3395
3396 #define DRV_NAME  "Ieee80211"
3397 void ieee80211_softmac_init(struct ieee80211_device *ieee)
3398 {
3399         int i;
3400         memset(&ieee->current_network, 0, sizeof(struct ieee80211_network));
3401
3402         ieee->state = IEEE80211_NOLINK;
3403         ieee->sync_scan_hurryup = 0;
3404         for(i = 0; i < 5; i++) {
3405           ieee->seq_ctrl[i] = 0;
3406         }
3407
3408         ieee->assoc_id = 0;
3409         ieee->queue_stop = 0;
3410         ieee->scanning = 0;
3411         ieee->softmac_features = 0; //so IEEE2100-like driver are happy
3412         ieee->wap_set = 0;
3413         ieee->ssid_set = 0;
3414         ieee->proto_started = 0;
3415         ieee->basic_rate = IEEE80211_DEFAULT_BASIC_RATE;
3416         ieee->rate = 3;
3417 //#ifdef ENABLE_LPS
3418         ieee->ps = IEEE80211_PS_MBCAST|IEEE80211_PS_UNICAST;
3419 //#else
3420 //      ieee->ps = IEEE80211_PS_DISABLED;
3421 //#endif
3422         ieee->sta_sleep = 0;
3423 //by amy
3424         ieee->bInactivePs = false;
3425         ieee->actscanning = false;
3426         ieee->ListenInterval = 2;
3427         ieee->NumRxDataInPeriod = 0; //YJ,add,080828
3428         ieee->NumRxBcnInPeriod = 0; //YJ,add,080828
3429         ieee->NumRxOkTotal = 0;//+by amy 080312
3430         ieee->NumRxUnicast = 0;//YJ,add,080828,for keep alive
3431         ieee->beinretry = false;
3432         ieee->bHwRadioOff = false;
3433 //by amy
3434 #ifdef _RTL8187_EXT_PATCH_
3435         ieee->iw_ext_mode = 999;
3436 #endif
3437
3438         init_mgmt_queue(ieee);
3439 #if 0
3440         init_timer(&ieee->scan_timer);
3441         ieee->scan_timer.data = (unsigned long)ieee;
3442         ieee->scan_timer.function = ieee80211_softmac_scan_cb;
3443 #endif
3444         ieee->tx_pending.txb = NULL;
3445
3446         init_timer(&ieee->associate_timer);
3447         ieee->associate_timer.data = (unsigned long)ieee;
3448         ieee->associate_timer.function = ieee80211_associate_abort_cb;
3449
3450         init_timer(&ieee->beacon_timer);
3451         ieee->beacon_timer.data = (unsigned long) ieee;
3452         ieee->beacon_timer.function = ieee80211_send_beacon_cb;
3453
3454 #ifdef PF_SYNCTHREAD
3455         ieee->wq = create_workqueue(DRV_NAME,0);
3456 #else
3457         ieee->wq = create_workqueue(DRV_NAME);
3458 #endif
3459         INIT_DELAYED_WORK(&ieee->start_ibss_wq,(void*) ieee80211_start_ibss_wq);
3460         INIT_WORK(&ieee->associate_complete_wq,(void*) ieee80211_associate_complete_wq);
3461         INIT_WORK(&ieee->associate_procedure_wq,(void*) ieee80211_associate_procedure_wq);
3462         INIT_DELAYED_WORK(&ieee->softmac_scan_wq,(void*) ieee80211_softmac_scan_wq);
3463         INIT_DELAYED_WORK(&ieee->associate_retry_wq,(void*) ieee80211_associate_retry_wq);
3464         INIT_WORK(&ieee->wx_sync_scan_wq,(void*) ieee80211_wx_sync_scan_wq);
3465 //      INIT_WORK(&ieee->watch_dog_wq,(void*) ieee80211_watch_dog_wq);
3466 //added by lawrence,20071118
3467 #ifdef _RTL8187_EXT_PATCH_
3468         INIT_WORK(&ieee->ext_stop_scan_wq,(void*) ieee80211_ext_stop_scan_wq);
3469         //INIT_WORK(&ieee->ext_send_beacon_wq,(void*) ieee80211_beacons_start,ieee);
3470         INIT_WORK(&ieee->ext_send_beacon_wq,(void*) ext_ieee80211_send_beacon_wq);
3471 #endif //_RTL8187_EXT_PATCH_
3472
3473         sema_init(&ieee->wx_sem, 1);
3474         sema_init(&ieee->scan_sem, 1);
3475
3476         spin_lock_init(&ieee->mgmt_tx_lock);
3477         spin_lock_init(&ieee->beacon_lock);
3478
3479         tasklet_init(&ieee->ps_task,
3480              (void(*)(unsigned long)) ieee80211_sta_ps,
3481              (unsigned long)ieee);
3482 #ifdef ENABLE_DOT11D
3483         ieee->pDot11dInfo = kmalloc(sizeof(RT_DOT11D_INFO), GFP_ATOMIC);
3484 #endif
3485 }
3486
3487 void ieee80211_softmac_free(struct ieee80211_device *ieee)
3488 {
3489         down(&ieee->wx_sem);
3490
3491         del_timer_sync(&ieee->associate_timer);
3492         cancel_delayed_work(&ieee->associate_retry_wq);
3493
3494
3495         //add for RF power on power of by lizhaoming 080512
3496         cancel_delayed_work(&ieee->GPIOChangeRFWorkItem);
3497
3498 #ifdef _RTL8187_EXT_PATCH_
3499         cancel_delayed_work(&ieee->ext_stop_scan_wq);
3500         cancel_delayed_work(&ieee->ext_send_beacon_wq);
3501 #endif
3502         destroy_workqueue(ieee->wq);
3503 #ifdef ENABLE_DOT11D
3504         if(NULL != ieee->pDot11dInfo)
3505                 kfree(ieee->pDot11dInfo);
3506 #endif
3507         up(&ieee->wx_sem);
3508 }
3509
3510 /********************************************************
3511  * Start of WPA code.                                   *
3512  * this is stolen from the ipw2200 driver               *
3513  ********************************************************/
3514
3515
3516 static int ieee80211_wpa_enable(struct ieee80211_device *ieee, int value)
3517 {
3518         /* This is called when wpa_supplicant loads and closes the driver
3519          * interface. */
3520         printk("%s WPA\n",value ? "enabling" : "disabling");
3521         ieee->wpa_enabled = value;
3522         return 0;
3523 }
3524
3525
3526 void ieee80211_wpa_assoc_frame(struct ieee80211_device *ieee, char *wpa_ie, int wpa_ie_len)
3527 {
3528         /* make sure WPA is enabled */
3529         ieee80211_wpa_enable(ieee, 1);
3530
3531         ieee80211_disassociate(ieee);
3532 }
3533
3534
3535 static int ieee80211_wpa_mlme(struct ieee80211_device *ieee, int command, int reason)
3536 {
3537
3538         int ret = 0;
3539
3540         switch (command) {
3541         case IEEE_MLME_STA_DEAUTH:
3542                 // silently ignore
3543                 break;
3544
3545         case IEEE_MLME_STA_DISASSOC:
3546                 ieee80211_disassociate(ieee);
3547                 break;
3548
3549         default:
3550                 printk("Unknown MLME request: %d\n", command);
3551                 ret = -EOPNOTSUPP;
3552         }
3553
3554         return ret;
3555 }
3556
3557
3558 static int ieee80211_wpa_set_wpa_ie(struct ieee80211_device *ieee,
3559                               struct ieee_param *param, int plen)
3560 {
3561         u8 *buf;
3562
3563         if (param->u.wpa_ie.len > MAX_WPA_IE_LEN ||
3564             (param->u.wpa_ie.len && param->u.wpa_ie.data == NULL))
3565                 return -EINVAL;
3566
3567         if (param->u.wpa_ie.len) {
3568                 buf = kmalloc(param->u.wpa_ie.len, GFP_KERNEL);
3569                 if (buf == NULL)
3570                         return -ENOMEM;
3571
3572                 memcpy(buf, param->u.wpa_ie.data, param->u.wpa_ie.len);
3573                 kfree(ieee->wpa_ie);
3574                 ieee->wpa_ie = buf;
3575                 ieee->wpa_ie_len = param->u.wpa_ie.len;
3576         } else {
3577                 kfree(ieee->wpa_ie);
3578                 ieee->wpa_ie = NULL;
3579                 ieee->wpa_ie_len = 0;
3580         }
3581
3582         ieee80211_wpa_assoc_frame(ieee, ieee->wpa_ie, ieee->wpa_ie_len);
3583         return 0;
3584 }
3585
3586 #define AUTH_ALG_OPEN_SYSTEM                    0x1
3587 #define AUTH_ALG_SHARED_KEY                     0x2
3588
3589 static int ieee80211_wpa_set_auth_algs(struct ieee80211_device *ieee, int value)
3590 {
3591
3592         struct ieee80211_security sec = {
3593                 .flags = SEC_AUTH_MODE,
3594         };
3595         int ret = 0;
3596
3597         if (value & AUTH_ALG_SHARED_KEY) {
3598                 sec.auth_mode = WLAN_AUTH_SHARED_KEY;
3599                 ieee->open_wep = 0;
3600         } else {
3601                 sec.auth_mode = WLAN_AUTH_OPEN;
3602                 ieee->open_wep = 1;
3603         }
3604
3605         if (ieee->set_security)
3606                 ieee->set_security(ieee->dev, &sec);
3607         else
3608                 ret = -EOPNOTSUPP;
3609
3610         return ret;
3611 }
3612
3613 static int ieee80211_wpa_set_param(struct ieee80211_device *ieee, u8 name, u32 value)
3614 {
3615         int ret=0;
3616         unsigned long flags;
3617
3618         switch (name) {
3619         case IEEE_PARAM_WPA_ENABLED:
3620                 ret = ieee80211_wpa_enable(ieee, value);
3621                 break;
3622
3623         case IEEE_PARAM_TKIP_COUNTERMEASURES:
3624                 ieee->tkip_countermeasures=value;
3625                 break;
3626
3627         case IEEE_PARAM_DROP_UNENCRYPTED: {
3628                 /* HACK:
3629                  *
3630                  * wpa_supplicant calls set_wpa_enabled when the driver
3631                  * is loaded and unloaded, regardless of if WPA is being
3632                  * used.  No other calls are made which can be used to
3633                  * determine if encryption will be used or not prior to
3634                  * association being expected.  If encryption is not being
3635                  * used, drop_unencrypted is set to false, else true -- we
3636                  * can use this to determine if the CAP_PRIVACY_ON bit should
3637                  * be set.
3638                  */
3639                 struct ieee80211_security sec = {
3640                         .flags = SEC_ENABLED,
3641                         .enabled = value,
3642                 };
3643                 ieee->drop_unencrypted = value;
3644                 /* We only change SEC_LEVEL for open mode. Others
3645                  * are set by ipw_wpa_set_encryption.
3646                  */
3647                 if (!value) {
3648                         sec.flags |= SEC_LEVEL;
3649                         sec.level = SEC_LEVEL_0;
3650                 }
3651                 else {
3652                         sec.flags |= SEC_LEVEL;
3653                         sec.level = SEC_LEVEL_1;
3654                 }
3655                 if (ieee->set_security)
3656                         ieee->set_security(ieee->dev, &sec);
3657                 break;
3658         }
3659
3660         case IEEE_PARAM_PRIVACY_INVOKED:
3661                 ieee->privacy_invoked=value;
3662                 break;
3663
3664         case IEEE_PARAM_AUTH_ALGS:
3665                 ret = ieee80211_wpa_set_auth_algs(ieee, value);
3666                 break;
3667
3668         case IEEE_PARAM_IEEE_802_1X:
3669                 ieee->ieee802_1x=value;
3670                 break;
3671         case IEEE_PARAM_WPAX_SELECT:
3672                 // added for WPA2 mixed mode
3673                 //printk(KERN_WARNING "------------------------>wpax value = %x\n", value);
3674                 spin_lock_irqsave(&ieee->wpax_suitlist_lock,flags);
3675                 ieee->wpax_type_set = 1;
3676                 ieee->wpax_type_notify = value;
3677                 spin_unlock_irqrestore(&ieee->wpax_suitlist_lock,flags);
3678                 break;
3679
3680         default:
3681                 printk("Unknown WPA param: %d\n",name);
3682                 ret = -EOPNOTSUPP;
3683         }
3684
3685         return ret;
3686 }
3687
3688 /* implementation borrowed from hostap driver */
3689
3690 static int ieee80211_wpa_set_encryption(struct ieee80211_device *ieee,
3691                                   struct ieee_param *param, int param_len)
3692 {
3693         int ret = 0;
3694
3695         struct ieee80211_crypto_ops *ops;
3696         struct ieee80211_crypt_data **crypt;
3697
3698         struct ieee80211_security sec = {
3699                 .flags = 0,
3700         };
3701
3702         param->u.crypt.err = 0;
3703         param->u.crypt.alg[IEEE_CRYPT_ALG_NAME_LEN - 1] = '\0';
3704
3705         if (param_len !=
3706             (int) ((char *) param->u.crypt.key - (char *) param) +
3707             param->u.crypt.key_len) {
3708                 printk("Len mismatch %d, %d\n", param_len,
3709                                param->u.crypt.key_len);
3710                 return -EINVAL;
3711         }
3712         if (param->sta_addr[0] == 0xff && param->sta_addr[1] == 0xff &&
3713             param->sta_addr[2] == 0xff && param->sta_addr[3] == 0xff &&
3714             param->sta_addr[4] == 0xff && param->sta_addr[5] == 0xff) {
3715                 if (param->u.crypt.idx >= WEP_KEYS)
3716                         return -EINVAL;
3717                 crypt = &ieee->crypt[param->u.crypt.idx];
3718         } else {
3719                 return -EINVAL;
3720         }
3721
3722         if (strcmp(param->u.crypt.alg, "none") == 0) {
3723                 if (crypt) {
3724                         sec.enabled = 0;
3725                         // FIXME FIXME
3726                         //sec.encrypt = 0;
3727                         sec.level = SEC_LEVEL_0;
3728                         sec.flags |= SEC_ENABLED | SEC_LEVEL;
3729                         ieee80211_crypt_delayed_deinit(ieee, crypt);
3730                 }
3731                 goto done;
3732         }
3733         sec.enabled = 1;
3734 // FIXME FIXME
3735 //      sec.encrypt = 1;
3736         sec.flags |= SEC_ENABLED;
3737
3738         /* IPW HW cannot build TKIP MIC, host decryption still needed. */
3739         if (!(ieee->host_encrypt || ieee->host_decrypt) &&
3740             strcmp(param->u.crypt.alg, "TKIP"))
3741                 goto skip_host_crypt;
3742
3743         ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
3744         if (ops == NULL && strcmp(param->u.crypt.alg, "WEP") == 0) {
3745                 request_module("ieee80211_crypt_wep");
3746                 ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
3747         } else if (ops == NULL && strcmp(param->u.crypt.alg, "TKIP") == 0) {
3748                 request_module("ieee80211_crypt_tkip");
3749                 ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
3750         } else if (ops == NULL && strcmp(param->u.crypt.alg, "CCMP") == 0) {
3751                 request_module("ieee80211_crypt_ccmp");
3752                 ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
3753         }
3754         if (ops == NULL) {
3755                 printk("unknown crypto alg '%s'\n", param->u.crypt.alg);
3756                 param->u.crypt.err = IEEE_CRYPT_ERR_UNKNOWN_ALG;
3757                 ret = -EINVAL;
3758                 goto done;
3759         }
3760
3761         if (*crypt == NULL || (*crypt)->ops != ops) {
3762                 struct ieee80211_crypt_data *new_crypt;
3763
3764                 ieee80211_crypt_delayed_deinit(ieee, crypt);
3765
3766                 new_crypt = (struct ieee80211_crypt_data *)
3767                         kmalloc(sizeof(*new_crypt), GFP_KERNEL);
3768                 if (new_crypt == NULL) {
3769                         ret = -ENOMEM;
3770                         goto done;
3771                 }
3772                 memset(new_crypt, 0, sizeof(struct ieee80211_crypt_data));
3773                 new_crypt->ops = ops;
3774                 if (new_crypt->ops && try_module_get(new_crypt->ops->owner))
3775                         new_crypt->priv =
3776                                 new_crypt->ops->init(param->u.crypt.idx);
3777
3778                 if (new_crypt->priv == NULL) {
3779                         kfree(new_crypt);
3780                         param->u.crypt.err = IEEE_CRYPT_ERR_CRYPT_INIT_FAILED;
3781                         ret = -EINVAL;
3782                         goto done;
3783                 }
3784
3785                 *crypt = new_crypt;
3786         }
3787
3788         if (param->u.crypt.key_len > 0 && (*crypt)->ops->set_key &&
3789             (*crypt)->ops->set_key(param->u.crypt.key,
3790                                    param->u.crypt.key_len, param->u.crypt.seq,
3791                                    (*crypt)->priv) < 0) {
3792                 printk("key setting failed\n");
3793                 param->u.crypt.err = IEEE_CRYPT_ERR_KEY_SET_FAILED;
3794                 ret = -EINVAL;
3795                 goto done;
3796         }
3797
3798  skip_host_crypt:
3799         if (param->u.crypt.set_tx) {
3800                 ieee->tx_keyidx = param->u.crypt.idx;
3801                 sec.active_key = param->u.crypt.idx;
3802                 sec.flags |= SEC_ACTIVE_KEY;
3803         } else
3804                 sec.flags &= ~SEC_ACTIVE_KEY;
3805
3806         if (param->u.crypt.alg != NULL) {
3807                 memcpy(sec.keys[param->u.crypt.idx],
3808                        param->u.crypt.key,
3809                        param->u.crypt.key_len);
3810                 sec.key_sizes[param->u.crypt.idx] = param->u.crypt.key_len;
3811                 sec.flags |= (1 << param->u.crypt.idx);
3812
3813                 if (strcmp(param->u.crypt.alg, "WEP") == 0) {
3814                         sec.flags |= SEC_LEVEL;
3815                         sec.level = SEC_LEVEL_1;
3816                 } else if (strcmp(param->u.crypt.alg, "TKIP") == 0) {
3817                         sec.flags |= SEC_LEVEL;
3818                         sec.level = SEC_LEVEL_2;
3819                 } else if (strcmp(param->u.crypt.alg, "CCMP") == 0) {
3820                         sec.flags |= SEC_LEVEL;
3821                         sec.level = SEC_LEVEL_3;
3822                 }
3823         }
3824  done:
3825         if (ieee->set_security)
3826                 ieee->set_security(ieee->dev, &sec);
3827
3828         /* Do not reset port if card is in Managed mode since resetting will
3829          * generate new IEEE 802.11 authentication which may end up in looping
3830          * with IEEE 802.1X.  If your hardware requires a reset after WEP
3831          * configuration (for example... Prism2), implement the reset_port in
3832          * the callbacks structures used to initialize the 802.11 stack. */
3833         if (ieee->reset_on_keychange &&
3834             ieee->iw_mode != IW_MODE_INFRA &&
3835             ieee->reset_port &&
3836             ieee->reset_port(ieee->dev)) {
3837                 printk("reset_port failed\n");
3838                 param->u.crypt.err = IEEE_CRYPT_ERR_CARD_CONF_FAILED;
3839                 return -EINVAL;
3840         }
3841
3842         return ret;
3843 }
3844
3845 int ieee80211_wpa_supplicant_ioctl(struct ieee80211_device *ieee, struct iw_point *p)
3846 {
3847         struct ieee_param *param;
3848         int ret=0;
3849
3850         down(&ieee->wx_sem);
3851         //IEEE_DEBUG_INFO("wpa_supplicant: len=%d\n", p->length);
3852
3853         if (p->length < sizeof(struct ieee_param) || !p->pointer){
3854                 ret = -EINVAL;
3855                 goto out;
3856         }
3857
3858         param = (struct ieee_param *)kmalloc(p->length, GFP_KERNEL);
3859         if (param == NULL){
3860                 ret = -ENOMEM;
3861                 goto out;
3862         }
3863         if (copy_from_user(param, p->pointer, p->length)) {
3864                 kfree(param);
3865                 ret = -EFAULT;
3866                 goto out;
3867         }
3868
3869         switch (param->cmd) {
3870
3871         case IEEE_CMD_SET_WPA_PARAM:
3872                 ret = ieee80211_wpa_set_param(ieee, param->u.wpa_param.name,
3873                                         param->u.wpa_param.value);
3874                 break;
3875
3876         case IEEE_CMD_SET_WPA_IE:
3877                 ret = ieee80211_wpa_set_wpa_ie(ieee, param, p->length);
3878                 break;
3879
3880         case IEEE_CMD_SET_ENCRYPTION:
3881                 ret = ieee80211_wpa_set_encryption(ieee, param, p->length);
3882                 break;
3883
3884         case IEEE_CMD_MLME:
3885                 ret = ieee80211_wpa_mlme(ieee, param->u.mlme.command,
3886                                    param->u.mlme.reason_code);
3887                 break;
3888
3889         default:
3890                 printk("Unknown WPA supplicant request: %d\n",param->cmd);
3891                 ret = -EOPNOTSUPP;
3892                 break;
3893         }
3894
3895         if (ret == 0 && copy_to_user(p->pointer, param, p->length))
3896                 ret = -EFAULT;
3897
3898         kfree(param);
3899 out:
3900         up(&ieee->wx_sem);
3901
3902         return ret;
3903 }
3904
3905 void notify_wx_assoc_event(struct ieee80211_device *ieee)
3906 {
3907         union iwreq_data wrqu;
3908         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
3909         if (ieee->state == IEEE80211_LINKED)
3910                 memcpy(wrqu.ap_addr.sa_data, ieee->current_network.bssid, ETH_ALEN);
3911         else
3912                 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
3913         wireless_send_event(ieee->dev, SIOCGIWAP, &wrqu, NULL);
3914 }
3915
3916
3917 #if 0
3918 EXPORT_SYMBOL(ieee80211_get_beacon);
3919 EXPORT_SYMBOL(ieee80211_wake_queue);
3920 EXPORT_SYMBOL(ieee80211_stop_queue);
3921 EXPORT_SYMBOL(ieee80211_reset_queue);
3922 EXPORT_SYMBOL(ieee80211_softmac_stop_protocol);
3923 EXPORT_SYMBOL(ieee80211_softmac_start_protocol);
3924 EXPORT_SYMBOL(ieee80211_is_shortslot);
3925 EXPORT_SYMBOL(ieee80211_is_54g);
3926 EXPORT_SYMBOL(ieee80211_wpa_supplicant_ioctl);
3927 EXPORT_SYMBOL(ieee80211_ps_tx_ack);
3928 EXPORT_SYMBOL(ieee80211_start_protocol);
3929 EXPORT_SYMBOL(ieee80211_stop_protocol);
3930 EXPORT_SYMBOL(notify_wx_assoc_event);
3931 EXPORT_SYMBOL(ieee80211_stop_send_beacons);
3932 EXPORT_SYMBOL(SendDisassociation);
3933 EXPORT_SYMBOL(ieee80211_disassociate);
3934 EXPORT_SYMBOL(ieee80211_start_scan);
3935 EXPORT_SYMBOL(ieee80211_softmac_ips_scan_syncro);
3936 #ifdef _RTL8187_EXT_PATCH_
3937 EXPORT_SYMBOL(ieee80211_ext_issue_assoc_req);
3938 EXPORT_SYMBOL(ieee80211_ext_issue_disassoc);
3939 EXPORT_SYMBOL(ieee80211_ext_issue_assoc_rsp);
3940 EXPORT_SYMBOL(softmac_mgmt_xmit);
3941 EXPORT_SYMBOL(ieee80211_ext_probe_resp_by_net);
3942 EXPORT_SYMBOL(ieee80211_start_scan);
3943 EXPORT_SYMBOL(ieee80211_stop_scan);
3944 EXPORT_SYMBOL(ieee80211_ext_send_11s_beacon);
3945 EXPORT_SYMBOL(ieee80211_rx_auth_rq);
3946 EXPORT_SYMBOL(ieee80211_associate_step1);
3947 #endif // _RTL8187_EXT_PATCH_
3948 EXPORT_SYMBOL(ieee80211_sta_ps_send_null_frame);
3949 #endif