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