spi/topcliff-pch: Fix Kconfig dependencies
[cascardo/linux.git] / drivers / staging / rtl8187se / ieee80211 / ieee80211_softmac.c
1 /* IEEE 802.11 SoftMAC layer
2  * Copyright (c) 2005 Andrea Merello <andrea.merello@gmail.com>
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 #include "ieee80211.h"
17
18 #include <linux/random.h>
19 #include <linux/delay.h>
20 #include <linux/slab.h>
21 #include <linux/interrupt.h>
22 #include <linux/uaccess.h>
23 #include <linux/etherdevice.h>
24
25 #include "dot11d.h"
26
27 short ieee80211_is_54g(const struct ieee80211_network *net)
28 {
29         return (net->rates_ex_len > 0) || (net->rates_len > 4);
30 }
31
32 short ieee80211_is_shortslot(const struct ieee80211_network *net)
33 {
34         return net->capability & WLAN_CAPABILITY_SHORT_SLOT;
35 }
36
37 /* returns the total length needed for placing the RATE MFIE
38  * tag and the EXTENDED RATE MFIE tag if needed.
39  * It encludes two bytes per tag for the tag itself and its len
40  */
41 static unsigned int ieee80211_MFIE_rate_len(struct ieee80211_device *ieee)
42 {
43         unsigned int rate_len = 0;
44
45         if (ieee->modulation & IEEE80211_CCK_MODULATION)
46                 rate_len = IEEE80211_CCK_RATE_LEN + 2;
47
48         if (ieee->modulation & IEEE80211_OFDM_MODULATION)
49
50                 rate_len += IEEE80211_OFDM_RATE_LEN + 2;
51
52         return rate_len;
53 }
54
55 /* place the MFIE rate, tag to the memory (double) poised.
56  * Then it updates the pointer so that it points after the new MFIE tag added.
57  */
58 static void ieee80211_MFIE_Brate(struct ieee80211_device *ieee, u8 **tag_p)
59 {
60         u8 *tag = *tag_p;
61
62         if (ieee->modulation & IEEE80211_CCK_MODULATION) {
63                 *tag++ = MFIE_TYPE_RATES;
64                 *tag++ = 4;
65                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_1MB;
66                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_2MB;
67                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_5MB;
68                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_11MB;
69         }
70
71         /* We may add an option for custom rates that specific HW might support */
72         *tag_p = tag;
73 }
74
75 static void ieee80211_MFIE_Grate(struct ieee80211_device *ieee, u8 **tag_p)
76 {
77         u8 *tag = *tag_p;
78
79                 if (ieee->modulation & IEEE80211_OFDM_MODULATION) {
80                 *tag++ = MFIE_TYPE_RATES_EX;
81                 *tag++ = 8;
82                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_6MB;
83                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_9MB;
84                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_12MB;
85                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_18MB;
86                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_24MB;
87                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_36MB;
88                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_48MB;
89                 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_54MB;
90
91         }
92         /* We may add an option for custom rates that specific HW might support */
93         *tag_p = tag;
94 }
95
96 static void ieee80211_WMM_Info(struct ieee80211_device *ieee, u8 **tag_p)
97 {
98         u8 *tag = *tag_p;
99
100         *tag++ = MFIE_TYPE_GENERIC; /* 0 */
101         *tag++ = 7;
102         *tag++ = 0x00;
103         *tag++ = 0x50;
104         *tag++ = 0xf2;
105         *tag++ = 0x02; /* 5 */
106         *tag++ = 0x00;
107         *tag++ = 0x01;
108 #ifdef SUPPORT_USPD
109         if (ieee->current_network.wmm_info & 0x80)
110                 *tag++ = 0x0f|MAX_SP_Len;
111         else
112                 *tag++ = MAX_SP_Len;
113 #else
114         *tag++ = MAX_SP_Len;
115 #endif
116         *tag_p = tag;
117 }
118
119 static void ieee80211_TURBO_Info(struct ieee80211_device *ieee, u8 **tag_p)
120 {
121         u8 *tag = *tag_p;
122         *tag++ = MFIE_TYPE_GENERIC; /* 0 */
123         *tag++ = 7;
124         *tag++ = 0x00;
125         *tag++ = 0xe0;
126         *tag++ = 0x4c;
127         *tag++ = 0x01; /* 5 */
128         *tag++ = 0x02;
129         *tag++ = 0x11;
130         *tag++ = 0x00;
131         *tag_p = tag;
132         printk(KERN_ALERT "This is enable turbo mode IE process\n");
133 }
134
135 static void enqueue_mgmt(struct ieee80211_device *ieee, struct sk_buff *skb)
136 {
137         int nh;
138         nh = (ieee->mgmt_queue_head + 1) % MGMT_QUEUE_NUM;
139
140         ieee->mgmt_queue_head = nh;
141         ieee->mgmt_queue_ring[nh] = skb;
142 }
143
144 static struct sk_buff *dequeue_mgmt(struct ieee80211_device *ieee)
145 {
146         struct sk_buff *ret;
147
148         if (ieee->mgmt_queue_tail == ieee->mgmt_queue_head)
149                 return NULL;
150
151         ret = ieee->mgmt_queue_ring[ieee->mgmt_queue_tail];
152
153         ieee->mgmt_queue_tail =
154                 (ieee->mgmt_queue_tail + 1) % MGMT_QUEUE_NUM;
155
156         return ret;
157 }
158
159 static void init_mgmt_queue(struct ieee80211_device *ieee)
160 {
161         ieee->mgmt_queue_tail = ieee->mgmt_queue_head = 0;
162 }
163
164 void ieee80211_sta_wakeup(struct ieee80211_device *ieee, short nl);
165
166 inline void softmac_mgmt_xmit(struct sk_buff *skb,
167                               struct ieee80211_device *ieee)
168 {
169         unsigned long flags;
170         short single = ieee->softmac_features & IEEE_SOFTMAC_SINGLE_QUEUE;
171         struct ieee80211_hdr_3addr  *header =
172                 (struct ieee80211_hdr_3addr  *) skb->data;
173
174         spin_lock_irqsave(&ieee->lock, flags);
175
176         /* called with 2nd param 0, no mgmt lock required */
177         ieee80211_sta_wakeup(ieee, 0);
178
179         if (single) {
180                 if (ieee->queue_stop) {
181                         enqueue_mgmt(ieee, skb);
182                 } else {
183                         header->seq_ctrl = cpu_to_le16(ieee->seq_ctrl[0]<<4);
184
185                         if (ieee->seq_ctrl[0] == 0xFFF)
186                                 ieee->seq_ctrl[0] = 0;
187                         else
188                                 ieee->seq_ctrl[0]++;
189
190                         /* avoid watchdog triggers */
191                         ieee->dev->trans_start = jiffies;
192                         ieee->softmac_data_hard_start_xmit(skb, ieee->dev, ieee->basic_rate);
193                 }
194
195                 spin_unlock_irqrestore(&ieee->lock, flags);
196         } else {
197                 spin_unlock_irqrestore(&ieee->lock, flags);
198                 spin_lock_irqsave(&ieee->mgmt_tx_lock, flags);
199
200                 header->seq_ctrl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
201
202                 if (ieee->seq_ctrl[0] == 0xFFF)
203                         ieee->seq_ctrl[0] = 0;
204                 else
205                         ieee->seq_ctrl[0]++;
206
207                 /* avoid watchdog triggers */
208                 ieee->dev->trans_start = jiffies;
209                 ieee->softmac_hard_start_xmit(skb, ieee->dev);
210
211                 spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags);
212         }
213 }
214
215 inline void softmac_ps_mgmt_xmit(struct sk_buff *skb,
216                                  struct ieee80211_device *ieee)
217 {
218         short single = ieee->softmac_features & IEEE_SOFTMAC_SINGLE_QUEUE;
219         struct ieee80211_hdr_3addr  *header =
220                 (struct ieee80211_hdr_3addr  *) skb->data;
221
222         if (single) {
223                 header->seq_ctrl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
224
225                 if (ieee->seq_ctrl[0] == 0xFFF)
226                         ieee->seq_ctrl[0] = 0;
227                 else
228                         ieee->seq_ctrl[0]++;
229
230                 /* avoid watchdog triggers */
231                 ieee->dev->trans_start = jiffies;
232                 ieee->softmac_data_hard_start_xmit(skb, ieee->dev, ieee->basic_rate);
233         } else {
234                 header->seq_ctrl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
235
236                 if (ieee->seq_ctrl[0] == 0xFFF)
237                         ieee->seq_ctrl[0] = 0;
238                 else
239                         ieee->seq_ctrl[0]++;
240
241                 /* avoid watchdog triggers */
242                 ieee->dev->trans_start = jiffies;
243                 ieee->softmac_hard_start_xmit(skb, ieee->dev);
244         }
245 }
246
247 inline struct sk_buff *
248 ieee80211_disassociate_skb(struct ieee80211_network *beacon,
249                            struct ieee80211_device *ieee, u8 asRsn)
250 {
251         struct sk_buff *skb;
252         struct ieee80211_disassoc_frame *disass;
253
254         skb = dev_alloc_skb(sizeof(struct ieee80211_disassoc_frame));
255         if (!skb)
256                 return NULL;
257
258         disass = (struct ieee80211_disassoc_frame *) skb_put(skb, sizeof(struct ieee80211_disassoc_frame));
259         disass->header.frame_control = cpu_to_le16(IEEE80211_STYPE_DISASSOC);
260         disass->header.duration_id = 0;
261
262         memcpy(disass->header.addr1, beacon->bssid, ETH_ALEN);
263         memcpy(disass->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
264         memcpy(disass->header.addr3, beacon->bssid, ETH_ALEN);
265
266         disass->reasoncode = asRsn;
267         return skb;
268 }
269
270 void SendDisassociation(struct ieee80211_device *ieee, u8 *asSta, u8 asRsn)
271 {
272         struct ieee80211_network *beacon = &ieee->current_network;
273         struct sk_buff *skb;
274         skb = ieee80211_disassociate_skb(beacon, ieee, asRsn);
275         if (skb)
276                 softmac_mgmt_xmit(skb, ieee);
277 }
278
279 inline struct sk_buff *ieee80211_probe_req(struct ieee80211_device *ieee)
280 {
281         unsigned int len, rate_len;
282         u8 *tag;
283         struct sk_buff *skb;
284         struct ieee80211_probe_request *req;
285
286         len = ieee->current_network.ssid_len;
287
288         rate_len = ieee80211_MFIE_rate_len(ieee);
289
290         skb = dev_alloc_skb(sizeof(struct ieee80211_probe_request) +
291                             2 + len + rate_len);
292         if (!skb)
293                 return NULL;
294
295         req = (struct ieee80211_probe_request *) skb_put(skb, sizeof(struct ieee80211_probe_request));
296         req->header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
297         req->header.duration_id = 0; /* FIXME: is this OK ? */
298
299         memset(req->header.addr1, 0xff, ETH_ALEN);
300         memcpy(req->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
301         memset(req->header.addr3, 0xff, ETH_ALEN);
302
303         tag = (u8 *) skb_put(skb, len + 2 + rate_len);
304
305         *tag++ = MFIE_TYPE_SSID;
306         *tag++ = len;
307         memcpy(tag, ieee->current_network.ssid, len);
308         tag += len;
309         ieee80211_MFIE_Brate(ieee, &tag);
310         ieee80211_MFIE_Grate(ieee, &tag);
311
312         return skb;
313 }
314
315 struct sk_buff *ieee80211_get_beacon_(struct ieee80211_device *ieee);
316
317 static void ieee80211_send_beacon(struct ieee80211_device *ieee)
318 {
319         struct sk_buff *skb;
320
321         skb = ieee80211_get_beacon_(ieee);
322
323         if (skb) {
324                 softmac_mgmt_xmit(skb, ieee);
325                 ieee->softmac_stats.tx_beacons++;
326                 dev_kfree_skb_any(skb);
327         }
328
329         ieee->beacon_timer.expires = jiffies +
330                 (MSECS(ieee->current_network.beacon_interval - 5));
331
332         if (ieee->beacon_txing)
333                 add_timer(&ieee->beacon_timer);
334 }
335
336
337 static void ieee80211_send_beacon_cb(unsigned long _ieee)
338 {
339         struct ieee80211_device *ieee =
340                 (struct ieee80211_device *) _ieee;
341         unsigned long flags;
342
343         spin_lock_irqsave(&ieee->beacon_lock, flags);
344         ieee80211_send_beacon(ieee);
345         spin_unlock_irqrestore(&ieee->beacon_lock, flags);
346 }
347
348 static void ieee80211_send_probe(struct ieee80211_device *ieee)
349 {
350         struct sk_buff *skb;
351
352         skb = ieee80211_probe_req(ieee);
353         if (skb) {
354                 softmac_mgmt_xmit(skb, ieee);
355                 ieee->softmac_stats.tx_probe_rq++;
356         }
357 }
358
359 static void ieee80211_send_probe_requests(struct ieee80211_device *ieee)
360 {
361         if (ieee->active_scan && (ieee->softmac_features & IEEE_SOFTMAC_PROBERQ)) {
362                 ieee80211_send_probe(ieee);
363                 ieee80211_send_probe(ieee);
364         }
365 }
366
367 /* this performs syncro scan blocking the caller until all channels
368  * in the allowed channel map has been checked.
369  */
370 static void ieee80211_softmac_scan_syncro(struct ieee80211_device *ieee)
371 {
372         short ch = 0;
373         u8 channel_map[MAX_CHANNEL_NUMBER+1];
374         memcpy(channel_map, GET_DOT11D_INFO(ieee)->channel_map, MAX_CHANNEL_NUMBER+1);
375         down(&ieee->scan_sem);
376
377         while (1) {
378                 do {
379                         ch++;
380                         if (ch > MAX_CHANNEL_NUMBER)
381                                 goto out; /* scan completed */
382
383                 } while (!channel_map[ch]);
384                 /* this function can be called in two situations
385                  * 1- We have switched to ad-hoc mode and we are
386                  *    performing a complete syncro scan before conclude
387                  *    there are no interesting cell and to create a
388                  *    new one. In this case the link state is
389                  *    IEEE80211_NOLINK until we found an interesting cell.
390                  *    If so the ieee8021_new_net, called by the RX path
391                  *    will set the state to IEEE80211_LINKED, so we stop
392                  *    scanning
393                  * 2- We are linked and the root uses run iwlist scan.
394                  *    So we switch to IEEE80211_LINKED_SCANNING to remember
395                  *    that we are still logically linked (not interested in
396                  *    new network events, despite for updating the net list,
397                  *    but we are temporarily 'unlinked' as the driver shall
398                  *    not filter RX frames and the channel is changing.
399                  * So the only situation in witch are interested is to check
400                  * if the state become LINKED because of the #1 situation
401                  */
402
403                 if (ieee->state == IEEE80211_LINKED)
404                         goto out;
405
406                 ieee->set_chan(ieee->dev, ch);
407                 if (channel_map[ch] == 1)
408                         ieee80211_send_probe_requests(ieee);
409
410                 /* this prevent excessive time wait when we
411                  * need to wait for a syncro scan to end..
412                  */
413                 if (ieee->sync_scan_hurryup)
414                         goto out;
415
416                 msleep_interruptible_rtl(IEEE80211_SOFTMAC_SCAN_TIME);
417         }
418 out:
419         ieee->sync_scan_hurryup = 0;
420         up(&ieee->scan_sem);
421         if (IS_DOT11D_ENABLE(ieee))
422                 DOT11D_ScanComplete(ieee);
423 }
424
425 void ieee80211_softmac_ips_scan_syncro(struct ieee80211_device *ieee)
426 {
427         int ch;
428         unsigned int watch_dog = 0;
429         u8 channel_map[MAX_CHANNEL_NUMBER+1];
430         memcpy(channel_map, GET_DOT11D_INFO(ieee)->channel_map, MAX_CHANNEL_NUMBER+1);
431         down(&ieee->scan_sem);
432         ch = ieee->current_network.channel;
433
434         while (1) {
435                 /* this function can be called in two situations
436                  * 1- We have switched to ad-hoc mode and we are
437                  *    performing a complete syncro scan before conclude
438                  *    there are no interesting cell and to create a
439                  *    new one. In this case the link state is
440                  *    IEEE80211_NOLINK until we found an interesting cell.
441                  *    If so the ieee8021_new_net, called by the RX path
442                  *    will set the state to IEEE80211_LINKED, so we stop
443                  *    scanning
444                  * 2- We are linked and the root uses run iwlist scan.
445                  *    So we switch to IEEE80211_LINKED_SCANNING to remember
446                  *    that we are still logically linked (not interested in
447                  *    new network events, despite for updating the net list,
448                  *    but we are temporarily 'unlinked' as the driver shall
449                  *    not filter RX frames and the channel is changing.
450                  * So the only situation in witch are interested is to check
451                  * if the state become LINKED because of the #1 situation
452                  */
453                 if (ieee->state == IEEE80211_LINKED)
454                         goto out;
455
456                 if (channel_map[ieee->current_network.channel] > 0)
457                         ieee->set_chan(ieee->dev, ieee->current_network.channel);
458
459                 if (channel_map[ieee->current_network.channel] == 1)
460                         ieee80211_send_probe_requests(ieee);
461
462                 msleep_interruptible_rtl(IEEE80211_SOFTMAC_SCAN_TIME);
463
464                 do {
465                         if (watch_dog++ >= MAX_CHANNEL_NUMBER)
466                                 goto out; /* scan completed */
467
468                         ieee->current_network.channel = (ieee->current_network.channel + 1)%MAX_CHANNEL_NUMBER;
469                 } while (!channel_map[ieee->current_network.channel]);
470         }
471 out:
472         ieee->actscanning = false;
473         up(&ieee->scan_sem);
474         if (IS_DOT11D_ENABLE(ieee))
475                 DOT11D_ScanComplete(ieee);
476 }
477
478 static void ieee80211_softmac_scan_wq(struct work_struct *work)
479 {
480         struct delayed_work *dwork = to_delayed_work(work);
481         struct ieee80211_device *ieee = container_of(dwork, struct ieee80211_device, softmac_scan_wq);
482         static short watchdog;
483         u8 channel_map[MAX_CHANNEL_NUMBER+1];
484         memcpy(channel_map, GET_DOT11D_INFO(ieee)->channel_map, MAX_CHANNEL_NUMBER+1);
485         down(&ieee->scan_sem);
486
487         do {
488                 ieee->current_network.channel =
489                         (ieee->current_network.channel + 1) % MAX_CHANNEL_NUMBER;
490                 if (watchdog++ > MAX_CHANNEL_NUMBER)
491                                 goto out; /* no good chans */
492         } while (!channel_map[ieee->current_network.channel]);
493
494         if (ieee->scanning == 0) {
495                 printk("error out, scanning = 0\n");
496                 goto out;
497         }
498         ieee->set_chan(ieee->dev, ieee->current_network.channel);
499         if (channel_map[ieee->current_network.channel] == 1)
500                 ieee80211_send_probe_requests(ieee);
501
502         queue_delayed_work(ieee->wq, &ieee->softmac_scan_wq, IEEE80211_SOFTMAC_SCAN_TIME);
503         up(&ieee->scan_sem);
504         return;
505 out:
506         ieee->actscanning = false;
507         watchdog = 0;
508         ieee->scanning = 0;
509         up(&ieee->scan_sem);
510
511         if (IS_DOT11D_ENABLE(ieee))
512                 DOT11D_ScanComplete(ieee);
513         return;
514 }
515
516 static void ieee80211_beacons_start(struct ieee80211_device *ieee)
517 {
518         unsigned long flags;
519
520         spin_lock_irqsave(&ieee->beacon_lock, flags);
521
522         ieee->beacon_txing = 1;
523         ieee80211_send_beacon(ieee);
524
525         spin_unlock_irqrestore(&ieee->beacon_lock, flags);
526 }
527
528 static void ieee80211_beacons_stop(struct ieee80211_device *ieee)
529 {
530         unsigned long flags;
531
532         spin_lock_irqsave(&ieee->beacon_lock, flags);
533
534         ieee->beacon_txing = 0;
535         del_timer_sync(&ieee->beacon_timer);
536
537         spin_unlock_irqrestore(&ieee->beacon_lock, flags);
538 }
539
540 void ieee80211_stop_send_beacons(struct ieee80211_device *ieee)
541 {
542         if (ieee->stop_send_beacons)
543                 ieee->stop_send_beacons(ieee->dev);
544         if (ieee->softmac_features & IEEE_SOFTMAC_BEACONS)
545                 ieee80211_beacons_stop(ieee);
546 }
547
548 void ieee80211_start_send_beacons(struct ieee80211_device *ieee)
549 {
550         if (ieee->start_send_beacons)
551                 ieee->start_send_beacons(ieee->dev);
552         if (ieee->softmac_features & IEEE_SOFTMAC_BEACONS)
553                 ieee80211_beacons_start(ieee);
554 }
555
556 static void ieee80211_softmac_stop_scan(struct ieee80211_device *ieee)
557 {
558         down(&ieee->scan_sem);
559
560         if (ieee->scanning == 1) {
561                 ieee->scanning = 0;
562                 cancel_delayed_work(&ieee->softmac_scan_wq);
563         }
564
565         up(&ieee->scan_sem);
566 }
567
568 void ieee80211_stop_scan(struct ieee80211_device *ieee)
569 {
570         if (ieee->softmac_features & IEEE_SOFTMAC_SCAN)
571                 ieee80211_softmac_stop_scan(ieee);
572         else
573                 ieee->stop_scan(ieee->dev);
574 }
575
576 /* called with ieee->lock held */
577 void ieee80211_rtl_start_scan(struct ieee80211_device *ieee)
578 {
579         if (IS_DOT11D_ENABLE(ieee)) {
580                 if (IS_COUNTRY_IE_VALID(ieee))
581                         RESET_CIE_WATCHDOG(ieee);
582         }
583
584         if (ieee->softmac_features & IEEE_SOFTMAC_SCAN) {
585                 if (ieee->scanning == 0) {
586                         ieee->scanning = 1;
587 #if 1
588                         queue_delayed_work(ieee->wq, &ieee->softmac_scan_wq, 0);
589 #endif
590                 }
591         }else
592                 ieee->start_scan(ieee->dev);
593 }
594
595 /* called with wx_sem held */
596 void ieee80211_start_scan_syncro(struct ieee80211_device *ieee)
597 {
598         if (IS_DOT11D_ENABLE(ieee)) {
599                 if (IS_COUNTRY_IE_VALID(ieee))
600                         RESET_CIE_WATCHDOG(ieee);
601         }
602         ieee->sync_scan_hurryup = 0;
603
604         if (ieee->softmac_features & IEEE_SOFTMAC_SCAN)
605                 ieee80211_softmac_scan_syncro(ieee);
606         else
607                 ieee->scan_syncro(ieee->dev);
608 }
609
610 inline struct sk_buff *
611 ieee80211_authentication_req(struct ieee80211_network *beacon,
612                              struct ieee80211_device *ieee, int challengelen)
613 {
614         struct sk_buff *skb;
615         struct ieee80211_authentication *auth;
616
617         skb = dev_alloc_skb(sizeof(struct ieee80211_authentication) + challengelen);
618
619         if (!skb)
620                 return NULL;
621
622         auth = (struct ieee80211_authentication *)
623                 skb_put(skb, sizeof(struct ieee80211_authentication));
624
625         auth->header.frame_ctl = IEEE80211_STYPE_AUTH;
626         if (challengelen)
627                 auth->header.frame_ctl |= IEEE80211_FCTL_WEP;
628
629         auth->header.duration_id = 0x013a; /* FIXME */
630
631         memcpy(auth->header.addr1, beacon->bssid, ETH_ALEN);
632         memcpy(auth->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
633         memcpy(auth->header.addr3, beacon->bssid, ETH_ALEN);
634
635         auth->algorithm = ieee->open_wep ? WLAN_AUTH_OPEN : WLAN_AUTH_SHARED_KEY;
636
637         auth->transaction = cpu_to_le16(ieee->associate_seq);
638         ieee->associate_seq++;
639
640         auth->status = cpu_to_le16(WLAN_STATUS_SUCCESS);
641
642         return skb;
643 }
644
645 static struct sk_buff *ieee80211_probe_resp(struct ieee80211_device *ieee,
646                                             u8 *dest)
647 {
648         u8 *tag;
649         int beacon_size;
650         struct ieee80211_probe_response *beacon_buf;
651         struct sk_buff *skb;
652         int encrypt;
653         int atim_len, erp_len;
654         struct ieee80211_crypt_data *crypt;
655
656         char *ssid = ieee->current_network.ssid;
657         int ssid_len = ieee->current_network.ssid_len;
658         int rate_len = ieee->current_network.rates_len+2;
659         int rate_ex_len = ieee->current_network.rates_ex_len;
660         int wpa_ie_len = ieee->wpa_ie_len;
661         if (rate_ex_len > 0)
662                 rate_ex_len += 2;
663
664         if (ieee->current_network.capability & WLAN_CAPABILITY_IBSS)
665                 atim_len = 4;
666         else
667                 atim_len = 0;
668
669         if (ieee80211_is_54g(&ieee->current_network))
670                 erp_len = 3;
671         else
672                 erp_len = 0;
673
674         beacon_size = sizeof(struct ieee80211_probe_response)+
675                 ssid_len
676                 +3 /* channel */
677                 +rate_len
678                 +rate_ex_len
679                 +atim_len
680                 +wpa_ie_len
681                 +erp_len;
682
683         skb = dev_alloc_skb(beacon_size);
684
685         if (!skb)
686                 return NULL;
687
688         beacon_buf = (struct ieee80211_probe_response *) skb_put(skb, beacon_size);
689
690         memcpy(beacon_buf->header.addr1, dest, ETH_ALEN);
691         memcpy(beacon_buf->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
692         memcpy(beacon_buf->header.addr3, ieee->current_network.bssid, ETH_ALEN);
693
694         beacon_buf->header.duration_id = 0; /* FIXME */
695         beacon_buf->beacon_interval =
696                 cpu_to_le16(ieee->current_network.beacon_interval);
697         beacon_buf->capability =
698                 cpu_to_le16(ieee->current_network.capability & WLAN_CAPABILITY_IBSS);
699
700         if (ieee->short_slot && (ieee->current_network.capability & WLAN_CAPABILITY_SHORT_SLOT))
701                 beacon_buf->capability |= cpu_to_le16(WLAN_CAPABILITY_SHORT_SLOT);
702
703         crypt = ieee->crypt[ieee->tx_keyidx];
704
705         encrypt = ieee->host_encrypt && crypt && crypt->ops &&
706                 ((0 == strcmp(crypt->ops->name, "WEP")) || wpa_ie_len);
707
708         if (encrypt)
709                 beacon_buf->capability |= cpu_to_le16(WLAN_CAPABILITY_PRIVACY);
710
711
712         beacon_buf->header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_PROBE_RESP);
713
714         beacon_buf->info_element.id = MFIE_TYPE_SSID;
715         beacon_buf->info_element.len = ssid_len;
716
717         tag = (u8 *) beacon_buf->info_element.data;
718
719         memcpy(tag, ssid, ssid_len);
720
721         tag += ssid_len;
722
723         *(tag++) = MFIE_TYPE_RATES;
724         *(tag++) = rate_len - 2;
725         memcpy(tag, ieee->current_network.rates, rate_len-2);
726         tag += rate_len - 2;
727
728         *(tag++) = MFIE_TYPE_DS_SET;
729         *(tag++) = 1;
730         *(tag++) = ieee->current_network.channel;
731
732         if (atim_len) {
733                 *(tag++) = MFIE_TYPE_IBSS_SET;
734                 *(tag++) = 2;
735                 *((u16 *)(tag)) = cpu_to_le16(ieee->current_network.atim_window);
736                 tag += 2;
737         }
738
739         if (erp_len) {
740                 *(tag++) = MFIE_TYPE_ERP;
741                 *(tag++) = 1;
742                 *(tag++) = 0;
743         }
744
745         if (rate_ex_len) {
746                 *(tag++) = MFIE_TYPE_RATES_EX;
747                 *(tag++) = rate_ex_len-2;
748                 memcpy(tag, ieee->current_network.rates_ex, rate_ex_len-2);
749                 tag += rate_ex_len - 2;
750         }
751
752         if (wpa_ie_len) {
753                 if (ieee->iw_mode == IW_MODE_ADHOC) {
754                         /* as Windows will set pairwise key same as the group
755                          * key which is not allowed in Linux, so set this for
756                          * IOT issue.
757                          */
758                         memcpy(&ieee->wpa_ie[14], &ieee->wpa_ie[8], 4);
759                 }
760
761                 memcpy(tag, ieee->wpa_ie, ieee->wpa_ie_len);
762         }
763         skb->dev = ieee->dev;
764         return skb;
765 }
766
767 static struct sk_buff *ieee80211_assoc_resp(struct ieee80211_device *ieee,
768                                             u8 *dest)
769 {
770         struct sk_buff *skb;
771         u8 *tag;
772
773         struct ieee80211_crypt_data *crypt;
774         struct ieee80211_assoc_response_frame *assoc;
775         short encrypt;
776
777         unsigned int rate_len = ieee80211_MFIE_rate_len(ieee);
778         int len = sizeof(struct ieee80211_assoc_response_frame) + rate_len;
779
780         skb = dev_alloc_skb(len);
781
782         if (!skb)
783                 return NULL;
784
785         assoc = (struct ieee80211_assoc_response_frame *)
786                 skb_put(skb, sizeof(struct ieee80211_assoc_response_frame));
787
788         assoc->header.frame_control = cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP);
789         memcpy(assoc->header.addr1, dest, ETH_ALEN);
790         memcpy(assoc->header.addr3, ieee->dev->dev_addr, ETH_ALEN);
791         memcpy(assoc->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
792         assoc->capability = cpu_to_le16(ieee->iw_mode == IW_MODE_MASTER ?
793                 WLAN_CAPABILITY_BSS : WLAN_CAPABILITY_IBSS);
794
795         if (ieee->short_slot)
796                 assoc->capability |= cpu_to_le16(WLAN_CAPABILITY_SHORT_SLOT);
797
798         if (ieee->host_encrypt)
799                 crypt = ieee->crypt[ieee->tx_keyidx];
800         else
801                 crypt = NULL;
802
803         encrypt = (crypt && crypt->ops);
804
805         if (encrypt)
806                 assoc->capability |= cpu_to_le16(WLAN_CAPABILITY_PRIVACY);
807
808         assoc->status = 0;
809         assoc->aid = cpu_to_le16(ieee->assoc_id);
810         if (ieee->assoc_id == 0x2007)
811                 ieee->assoc_id = 0;
812         else
813                 ieee->assoc_id++;
814
815         tag = (u8 *) skb_put(skb, rate_len);
816
817         ieee80211_MFIE_Brate(ieee, &tag);
818         ieee80211_MFIE_Grate(ieee, &tag);
819
820         return skb;
821 }
822
823 static struct sk_buff *ieee80211_auth_resp(struct ieee80211_device *ieee,
824                                            int status, u8 *dest)
825 {
826         struct sk_buff *skb;
827         struct ieee80211_authentication *auth;
828
829         skb = dev_alloc_skb(sizeof(struct ieee80211_authentication)+1);
830
831         if (!skb)
832                 return NULL;
833
834         skb->len = sizeof(struct ieee80211_authentication);
835
836         auth = (struct ieee80211_authentication *)skb->data;
837
838         auth->status = cpu_to_le16(status);
839         auth->transaction = cpu_to_le16(2);
840         auth->algorithm = cpu_to_le16(WLAN_AUTH_OPEN);
841
842         memcpy(auth->header.addr3, ieee->dev->dev_addr, ETH_ALEN);
843         memcpy(auth->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
844         memcpy(auth->header.addr1, dest, ETH_ALEN);
845         auth->header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_AUTH);
846         return skb;
847 }
848
849 static struct sk_buff *ieee80211_null_func(struct ieee80211_device *ieee, short pwr)
850 {
851         struct sk_buff *skb;
852         struct ieee80211_hdr_3addr *hdr;
853
854         skb = dev_alloc_skb(sizeof(struct ieee80211_hdr_3addr));
855
856         if (!skb)
857                 return NULL;
858
859         hdr = (struct ieee80211_hdr_3addr *)skb_put(skb, sizeof(struct ieee80211_hdr_3addr));
860
861         memcpy(hdr->addr1, ieee->current_network.bssid, ETH_ALEN);
862         memcpy(hdr->addr2, ieee->dev->dev_addr, ETH_ALEN);
863         memcpy(hdr->addr3, ieee->current_network.bssid, ETH_ALEN);
864
865         hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
866                 IEEE80211_STYPE_NULLFUNC | IEEE80211_FCTL_TODS |
867                 (pwr ? IEEE80211_FCTL_PM:0));
868
869         return skb;
870 }
871
872 static void ieee80211_resp_to_assoc_rq(struct ieee80211_device *ieee, u8 *dest)
873 {
874         struct sk_buff *buf = ieee80211_assoc_resp(ieee, dest);
875
876         if (buf) {
877                 softmac_mgmt_xmit(buf, ieee);
878                 dev_kfree_skb_any(buf);
879         }
880 }
881
882 static void ieee80211_resp_to_auth(struct ieee80211_device *ieee, int s, u8 *dest)
883 {
884         struct sk_buff *buf = ieee80211_auth_resp(ieee, s, dest);
885
886         if (buf) {
887                 softmac_mgmt_xmit(buf, ieee);
888                 dev_kfree_skb_any(buf);
889         }
890 }
891
892 static void ieee80211_resp_to_probe(struct ieee80211_device *ieee, u8 *dest)
893 {
894         struct sk_buff *buf = ieee80211_probe_resp(ieee, dest);
895
896         if (buf) {
897                 softmac_mgmt_xmit(buf, ieee);
898                 dev_kfree_skb_any(buf);
899         }
900 }
901
902 inline struct sk_buff *
903 ieee80211_association_req(struct ieee80211_network *beacon,
904                           struct ieee80211_device *ieee)
905 {
906         struct sk_buff *skb;
907
908         struct ieee80211_assoc_request_frame *hdr;
909         u8 *tag;
910         unsigned int wpa_len = beacon->wpa_ie_len;
911 #if 1
912         /* for testing purpose */
913         unsigned int rsn_len = beacon->rsn_ie_len;
914 #endif
915         unsigned int rate_len = ieee80211_MFIE_rate_len(ieee);
916         unsigned int wmm_info_len = beacon->QoS_Enable?9:0;
917         unsigned int turbo_info_len = beacon->Turbo_Enable?9:0;
918
919         u8  encry_proto = ieee->wpax_type_notify & 0xff;
920
921         int len = 0;
922
923         /* [0] Notify type of encryption: WPA/WPA2
924          * [1] pair wise type
925          * [2] authen type
926          */
927         if (ieee->wpax_type_set) {
928                 if (IEEE_PROTO_WPA == encry_proto) {
929                         rsn_len = 0;
930                 } else if (IEEE_PROTO_RSN == encry_proto) {
931                         wpa_len = 0;
932                 }
933         }
934         len = sizeof(struct ieee80211_assoc_request_frame)+
935                 + beacon->ssid_len /* essid tagged val */
936                 + rate_len /* rates tagged val */
937                 + wpa_len
938                 + rsn_len
939                 + wmm_info_len
940                 + turbo_info_len;
941
942         skb = dev_alloc_skb(len);
943
944         if (!skb)
945                 return NULL;
946
947         hdr = (struct ieee80211_assoc_request_frame *)
948                 skb_put(skb, sizeof(struct ieee80211_assoc_request_frame));
949
950         hdr->header.frame_control = IEEE80211_STYPE_ASSOC_REQ;
951         hdr->header.duration_id = 37; /* FIXME */
952         memcpy(hdr->header.addr1, beacon->bssid, ETH_ALEN);
953         memcpy(hdr->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
954         memcpy(hdr->header.addr3, beacon->bssid, ETH_ALEN);
955         memcpy(ieee->ap_mac_addr, beacon->bssid, ETH_ALEN); /* for HW security */
956
957         hdr->capability = cpu_to_le16(WLAN_CAPABILITY_BSS);
958         if (beacon->capability & WLAN_CAPABILITY_PRIVACY)
959                 hdr->capability |= cpu_to_le16(WLAN_CAPABILITY_PRIVACY);
960         if (beacon->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
961                 hdr->capability |= cpu_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE);
962
963         if (ieee->short_slot)
964                 hdr->capability |= cpu_to_le16(WLAN_CAPABILITY_SHORT_SLOT);
965
966         hdr->listen_interval = 0xa; /* FIXME */
967
968         hdr->info_element.id = MFIE_TYPE_SSID;
969
970         hdr->info_element.len = beacon->ssid_len;
971         tag = skb_put(skb, beacon->ssid_len);
972         memcpy(tag, beacon->ssid, beacon->ssid_len);
973
974         tag = skb_put(skb, rate_len);
975
976         ieee80211_MFIE_Brate(ieee, &tag);
977         ieee80211_MFIE_Grate(ieee, &tag);
978
979         /* add rsn==0 condition for ap's mix security mode(wpa+wpa2)
980          * choose AES encryption as default algorithm while using mixed mode.
981          */
982
983         tag = skb_put(skb, ieee->wpa_ie_len);
984         memcpy(tag, ieee->wpa_ie, ieee->wpa_ie_len);
985
986         tag = skb_put(skb, wmm_info_len);
987         if (wmm_info_len)
988           ieee80211_WMM_Info(ieee, &tag);
989
990         tag = skb_put(skb, turbo_info_len);
991         if (turbo_info_len)
992                 ieee80211_TURBO_Info(ieee, &tag);
993
994         return skb;
995 }
996
997 void ieee80211_associate_abort(struct ieee80211_device *ieee)
998 {
999         unsigned long flags;
1000         spin_lock_irqsave(&ieee->lock, flags);
1001
1002         ieee->associate_seq++;
1003
1004         /* don't scan, and avoid to have the RX path possibly
1005          * try again to associate. Even do not react to AUTH or
1006          * ASSOC response. Just wait for the retry wq to be scheduled.
1007          * Here we will check if there are good nets to associate
1008          * with, so we retry or just get back to NO_LINK and scanning
1009          */
1010         if (ieee->state == IEEE80211_ASSOCIATING_AUTHENTICATING) {
1011                 IEEE80211_DEBUG_MGMT("Authentication failed\n");
1012                 ieee->softmac_stats.no_auth_rs++;
1013         } else {
1014                 IEEE80211_DEBUG_MGMT("Association failed\n");
1015                 ieee->softmac_stats.no_ass_rs++;
1016         }
1017
1018         ieee->state = IEEE80211_ASSOCIATING_RETRY;
1019
1020         queue_delayed_work(ieee->wq, &ieee->associate_retry_wq, IEEE80211_SOFTMAC_ASSOC_RETRY_TIME);
1021
1022         spin_unlock_irqrestore(&ieee->lock, flags);
1023 }
1024
1025 static void ieee80211_associate_abort_cb(unsigned long dev)
1026 {
1027         ieee80211_associate_abort((struct ieee80211_device *) dev);
1028 }
1029
1030 static void ieee80211_associate_step1(struct ieee80211_device *ieee)
1031 {
1032         struct ieee80211_network *beacon = &ieee->current_network;
1033         struct sk_buff *skb;
1034
1035         IEEE80211_DEBUG_MGMT("Stopping scan\n");
1036         ieee->softmac_stats.tx_auth_rq++;
1037         skb = ieee80211_authentication_req(beacon, ieee, 0);
1038         if (!skb) {
1039                 ieee80211_associate_abort(ieee);
1040         } else {
1041                 ieee->state = IEEE80211_ASSOCIATING_AUTHENTICATING;
1042                 IEEE80211_DEBUG_MGMT("Sending authentication request\n");
1043                 softmac_mgmt_xmit(skb, ieee);
1044                 /* BUGON when you try to add_timer twice, using mod_timer may
1045                  * be better.
1046                  */
1047                 if (!timer_pending(&ieee->associate_timer)) {
1048                         ieee->associate_timer.expires = jiffies + (HZ / 2);
1049                         add_timer(&ieee->associate_timer);
1050                 }
1051                 /* If call dev_kfree_skb_any,a warning will ocur....
1052                  * KERNEL: assertion (!atomic_read(&skb->users)) failed at
1053                  * net/core/dev.c (1708)
1054                  */
1055         }
1056 }
1057
1058 static void ieee80211_rtl_auth_challenge(struct ieee80211_device *ieee, u8 *challenge,
1059                                   int chlen)
1060 {
1061         u8 *c;
1062         struct sk_buff *skb;
1063         struct ieee80211_network *beacon = &ieee->current_network;
1064         del_timer_sync(&ieee->associate_timer);
1065         ieee->associate_seq++;
1066         ieee->softmac_stats.tx_auth_rq++;
1067
1068         skb = ieee80211_authentication_req(beacon, ieee, chlen+2);
1069         if (!skb)
1070                 ieee80211_associate_abort(ieee);
1071         else {
1072                 c = skb_put(skb, chlen+2);
1073                 *(c++) = MFIE_TYPE_CHALLENGE;
1074                 *(c++) = chlen;
1075                 memcpy(c, challenge, chlen);
1076
1077                 IEEE80211_DEBUG_MGMT("Sending authentication challenge response\n");
1078
1079                 ieee80211_encrypt_fragment(ieee, skb, sizeof(struct ieee80211_hdr_3addr));
1080
1081                 softmac_mgmt_xmit(skb, ieee);
1082                 if (!timer_pending(&ieee->associate_timer)) {
1083                 ieee->associate_timer.expires = jiffies + (HZ / 2);
1084                 add_timer(&ieee->associate_timer);
1085                 }
1086                 dev_kfree_skb_any(skb);
1087         }
1088         kfree(challenge);
1089 }
1090
1091 static void ieee80211_associate_step2(struct ieee80211_device *ieee)
1092 {
1093         struct sk_buff *skb;
1094         struct ieee80211_network *beacon = &ieee->current_network;
1095
1096         del_timer_sync(&ieee->associate_timer);
1097
1098         IEEE80211_DEBUG_MGMT("Sending association request\n");
1099         ieee->softmac_stats.tx_ass_rq++;
1100         skb = ieee80211_association_req(beacon, ieee);
1101         if (!skb)
1102                 ieee80211_associate_abort(ieee);
1103         else {
1104                 softmac_mgmt_xmit(skb, ieee);
1105                 if (!timer_pending(&ieee->associate_timer)) {
1106                 ieee->associate_timer.expires = jiffies + (HZ / 2);
1107                 add_timer(&ieee->associate_timer);
1108                 }
1109         }
1110 }
1111
1112 static void ieee80211_associate_complete_wq(struct work_struct *work)
1113 {
1114         struct ieee80211_device *ieee = container_of(work, struct ieee80211_device, associate_complete_wq);
1115
1116         printk(KERN_INFO "Associated successfully\n");
1117         if (ieee80211_is_54g(&ieee->current_network) &&
1118                 (ieee->modulation & IEEE80211_OFDM_MODULATION)) {
1119                 ieee->rate = 540;
1120                 printk(KERN_INFO"Using G rates\n");
1121         } else {
1122                 ieee->rate = 110;
1123                 printk(KERN_INFO"Using B rates\n");
1124         }
1125         ieee->link_change(ieee->dev);
1126         notify_wx_assoc_event(ieee);
1127         if (ieee->data_hard_resume)
1128                 ieee->data_hard_resume(ieee->dev);
1129         netif_carrier_on(ieee->dev);
1130 }
1131
1132 static void ieee80211_associate_complete(struct ieee80211_device *ieee)
1133 {
1134         del_timer_sync(&ieee->associate_timer);
1135
1136         ieee->state = IEEE80211_LINKED;
1137         IEEE80211_DEBUG_MGMT("Successfully associated\n");
1138
1139         queue_work(ieee->wq, &ieee->associate_complete_wq);
1140 }
1141
1142 static void ieee80211_associate_procedure_wq(struct work_struct *work)
1143 {
1144         struct ieee80211_device *ieee = container_of(work, struct ieee80211_device, associate_procedure_wq);
1145
1146         ieee->sync_scan_hurryup = 1;
1147         down(&ieee->wx_sem);
1148
1149         if (ieee->data_hard_stop)
1150                 ieee->data_hard_stop(ieee->dev);
1151
1152         ieee80211_stop_scan(ieee);
1153         ieee->set_chan(ieee->dev, ieee->current_network.channel);
1154
1155         ieee->associate_seq = 1;
1156         ieee80211_associate_step1(ieee);
1157
1158         up(&ieee->wx_sem);
1159 }
1160
1161 inline void ieee80211_softmac_new_net(struct ieee80211_device *ieee,
1162                                       struct ieee80211_network *net)
1163 {
1164         u8 tmp_ssid[IW_ESSID_MAX_SIZE+1];
1165         int tmp_ssid_len = 0;
1166
1167         short apset, ssidset, ssidbroad, apmatch, ssidmatch;
1168
1169         /* we are interested in new new only if we are not associated
1170          * and we are not associating / authenticating
1171          */
1172         if (ieee->state != IEEE80211_NOLINK)
1173                 return;
1174
1175         if ((ieee->iw_mode == IW_MODE_INFRA) && !(net->capability & WLAN_CAPABILITY_BSS))
1176                 return;
1177
1178         if ((ieee->iw_mode == IW_MODE_ADHOC) && !(net->capability & WLAN_CAPABILITY_IBSS))
1179                 return;
1180
1181         if (ieee->iw_mode == IW_MODE_INFRA || ieee->iw_mode == IW_MODE_ADHOC) {
1182                 /* if the user specified the AP MAC, we need also the essid
1183                  * This could be obtained by beacons or, if the network does not
1184                  * broadcast it, it can be put manually.
1185                  */
1186                 apset = ieee->wap_set;
1187                 ssidset = ieee->ssid_set;
1188                 ssidbroad =  !(net->ssid_len == 0 || net->ssid[0] == '\0');
1189                 apmatch = (memcmp(ieee->current_network.bssid, net->bssid, ETH_ALEN) == 0);
1190
1191                 if (ieee->current_network.ssid_len != net->ssid_len)
1192                         ssidmatch = 0;
1193                 else
1194                         ssidmatch = (0 == strncmp(ieee->current_network.ssid, net->ssid, net->ssid_len));
1195
1196                 /* if the user set the AP check if match.
1197                  * if the network does not broadcast essid we check the user
1198                  * supplied ANY essid
1199                  * if the network does broadcast and the user does not set essid
1200                  * it is OK
1201                  * if the network does broadcast and the user did set essid
1202                  * chech if essid match
1203                  * (apset && apmatch && ((ssidset && ssidbroad && ssidmatch) ||
1204                  *  (ssidbroad && !ssidset) || (!ssidbroad && ssidset))) ||
1205                  * if the ap is not set, check that the user set the bssid and
1206                  * the network does broadcast and that those two bssid matches
1207                  * (!apset && ssidset && ssidbroad && ssidmatch)
1208                  */
1209                 if ((apset && apmatch && ((ssidset && ssidbroad && ssidmatch) ||
1210                      (ssidbroad && !ssidset) || (!ssidbroad && ssidset))) ||
1211                     (!apset && ssidset && ssidbroad && ssidmatch)) {
1212                         /* if the essid is hidden replace it with the
1213                          * essid provided by the user.
1214                          */
1215                         if (!ssidbroad) {
1216                                 strncpy(tmp_ssid, ieee->current_network.ssid, IW_ESSID_MAX_SIZE);
1217                                 tmp_ssid_len = ieee->current_network.ssid_len;
1218                         }
1219                         memcpy(&ieee->current_network, net, sizeof(struct ieee80211_network));
1220
1221                         if (!ssidbroad) {
1222                                 strncpy(ieee->current_network.ssid, tmp_ssid, IW_ESSID_MAX_SIZE);
1223                                 ieee->current_network.ssid_len = tmp_ssid_len;
1224                         }
1225                         printk(KERN_INFO"Linking with %s: channel is %d\n", ieee->current_network.ssid, ieee->current_network.channel);
1226
1227                         if (ieee->iw_mode == IW_MODE_INFRA) {
1228                                 ieee->state = IEEE80211_ASSOCIATING;
1229                                 ieee->beinretry = false;
1230                                 queue_work(ieee->wq, &ieee->associate_procedure_wq);
1231                         } else {
1232                                 if (ieee80211_is_54g(&ieee->current_network) &&
1233                                                 (ieee->modulation & IEEE80211_OFDM_MODULATION)) {
1234                                         ieee->rate = 540;
1235                                         printk(KERN_INFO"Using G rates\n");
1236                                 } else {
1237                                         ieee->rate = 110;
1238                                         printk(KERN_INFO"Using B rates\n");
1239                                 }
1240                                 ieee->state = IEEE80211_LINKED;
1241                                 ieee->beinretry = false;
1242                         }
1243                 }
1244         }
1245 }
1246
1247 void ieee80211_softmac_check_all_nets(struct ieee80211_device *ieee)
1248 {
1249         unsigned long flags;
1250         struct ieee80211_network *target;
1251
1252         spin_lock_irqsave(&ieee->lock, flags);
1253         list_for_each_entry(target, &ieee->network_list, list) {
1254                 /* if the state become different that NOLINK means
1255                  * we had found what we are searching for
1256                  */
1257                 if (ieee->state != IEEE80211_NOLINK)
1258                         break;
1259
1260                 if (ieee->scan_age == 0 || time_after(target->last_scanned + ieee->scan_age, jiffies))
1261                         ieee80211_softmac_new_net(ieee, target);
1262         }
1263         spin_unlock_irqrestore(&ieee->lock, flags);
1264 }
1265
1266 static inline u16 auth_parse(struct sk_buff *skb, u8 **challenge, int *chlen)
1267 {
1268         struct ieee80211_authentication *a;
1269         u8 *t;
1270         if (skb->len < (sizeof(struct ieee80211_authentication) - sizeof(struct ieee80211_info_element))) {
1271                 IEEE80211_DEBUG_MGMT("invalid len in auth resp: %d\n", skb->len);
1272                 return 0xcafe;
1273         }
1274         *challenge = NULL;
1275         a = (struct ieee80211_authentication *) skb->data;
1276         if (skb->len > (sizeof(struct ieee80211_authentication) + 3)) {
1277                 t = skb->data + sizeof(struct ieee80211_authentication);
1278
1279                 if (*(t++) == MFIE_TYPE_CHALLENGE) {
1280                         *chlen = *(t++);
1281                         *challenge = kmemdup(t, *chlen, GFP_ATOMIC);
1282                         if (!*challenge)
1283                                 return -ENOMEM;
1284                 }
1285         }
1286         return cpu_to_le16(a->status);
1287 }
1288
1289 static int auth_rq_parse(struct sk_buff *skb, u8 *dest)
1290 {
1291         struct ieee80211_authentication *a;
1292
1293         if (skb->len < (sizeof(struct ieee80211_authentication) - sizeof(struct ieee80211_info_element))) {
1294                 IEEE80211_DEBUG_MGMT("invalid len in auth request: %d\n", skb->len);
1295                 return -1;
1296         }
1297         a = (struct ieee80211_authentication *) skb->data;
1298
1299         memcpy(dest, a->header.addr2, ETH_ALEN);
1300
1301         if (le16_to_cpu(a->algorithm) != WLAN_AUTH_OPEN)
1302                 return  WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
1303
1304         return WLAN_STATUS_SUCCESS;
1305 }
1306
1307 static short probe_rq_parse(struct ieee80211_device *ieee, struct sk_buff *skb,
1308                             u8 *src)
1309 {
1310         u8 *tag;
1311         u8 *skbend;
1312         u8 *ssid = NULL;
1313         u8 ssidlen = 0;
1314
1315         struct ieee80211_hdr_3addr   *header =
1316                 (struct ieee80211_hdr_3addr   *) skb->data;
1317
1318         if (skb->len < sizeof(struct ieee80211_hdr_3addr))
1319                 return -1; /* corrupted */
1320
1321         memcpy(src, header->addr2, ETH_ALEN);
1322
1323         skbend = (u8 *)skb->data + skb->len;
1324
1325         tag = skb->data + sizeof(struct ieee80211_hdr_3addr);
1326
1327         while (tag+1 < skbend) {
1328                 if (*tag == 0) {
1329                         ssid = tag+2;
1330                         ssidlen = *(tag+1);
1331                         break;
1332                 }
1333                 tag++; /* point to the len field */
1334                 tag = tag + *(tag); /* point to the last data byte of the tag */
1335                 tag++; /* point to the next tag */
1336         }
1337
1338         if (ssidlen == 0)
1339                 return 1;
1340
1341         if (!ssid)
1342                  return 1; /* ssid not found in tagged param */
1343
1344         return (!strncmp(ssid, ieee->current_network.ssid, ssidlen));
1345
1346 }
1347
1348 static int assoc_rq_parse(struct sk_buff *skb, u8 *dest)
1349 {
1350         struct ieee80211_assoc_request_frame *a;
1351
1352         if (skb->len < (sizeof(struct ieee80211_assoc_request_frame) -
1353                 sizeof(struct ieee80211_info_element))) {
1354
1355                 IEEE80211_DEBUG_MGMT("invalid len in auth request:%d\n", skb->len);
1356                 return -1;
1357         }
1358
1359         a = (struct ieee80211_assoc_request_frame *) skb->data;
1360
1361         memcpy(dest, a->header.addr2, ETH_ALEN);
1362
1363         return 0;
1364 }
1365
1366 static inline u16 assoc_parse(struct sk_buff *skb, int *aid)
1367 {
1368         struct ieee80211_assoc_response_frame *a;
1369         if (skb->len < sizeof(struct ieee80211_assoc_response_frame)) {
1370                 IEEE80211_DEBUG_MGMT("invalid len in auth resp: %d\n", skb->len);
1371                 return 0xcafe;
1372         }
1373
1374         a = (struct ieee80211_assoc_response_frame *) skb->data;
1375         *aid = le16_to_cpu(a->aid) & 0x3fff;
1376         return le16_to_cpu(a->status);
1377 }
1378
1379 static inline void ieee80211_rx_probe_rq(struct ieee80211_device *ieee,
1380                                          struct sk_buff *skb)
1381 {
1382         u8 dest[ETH_ALEN];
1383
1384         ieee->softmac_stats.rx_probe_rq++;
1385         if (probe_rq_parse(ieee, skb, dest)) {
1386                 ieee->softmac_stats.tx_probe_rs++;
1387                 ieee80211_resp_to_probe(ieee, dest);
1388         }
1389 }
1390
1391 inline void ieee80211_rx_auth_rq(struct ieee80211_device *ieee,
1392                                  struct sk_buff *skb)
1393 {
1394         u8 dest[ETH_ALEN];
1395         int status;
1396         ieee->softmac_stats.rx_auth_rq++;
1397
1398         status = auth_rq_parse(skb, dest);
1399         if (status != -1)
1400                 ieee80211_resp_to_auth(ieee, status, dest);
1401 }
1402
1403 inline void
1404 ieee80211_rx_assoc_rq(struct ieee80211_device *ieee, struct sk_buff *skb)
1405 {
1406
1407         u8 dest[ETH_ALEN];
1408
1409         ieee->softmac_stats.rx_ass_rq++;
1410         if (assoc_rq_parse(skb, dest) != -1)
1411                 ieee80211_resp_to_assoc_rq(ieee, dest);
1412
1413
1414         printk(KERN_INFO"New client associated: %pM\n", dest);
1415 }
1416
1417 void ieee80211_sta_ps_send_null_frame(struct ieee80211_device *ieee, short pwr)
1418 {
1419         struct sk_buff *buf = ieee80211_null_func(ieee, pwr);
1420
1421         if (buf)
1422                 softmac_ps_mgmt_xmit(buf, ieee);
1423 }
1424
1425 static short ieee80211_sta_ps_sleep(struct ieee80211_device *ieee, u32 *time_h,
1426                              u32 *time_l)
1427 {
1428         int timeout = 0;
1429
1430         u8 dtim;
1431         dtim = ieee->current_network.dtim_data;
1432
1433         if (!(dtim & IEEE80211_DTIM_VALID))
1434                 return 0;
1435         else
1436                 timeout = ieee->current_network.beacon_interval;
1437
1438         ieee->current_network.dtim_data = IEEE80211_DTIM_INVALID;
1439
1440         if (dtim & ((IEEE80211_DTIM_UCAST | IEEE80211_DTIM_MBCAST) & ieee->ps))
1441                 return 2;
1442
1443         if (!time_after(jiffies, ieee->dev->trans_start + MSECS(timeout)))
1444                 return 0;
1445
1446         if (!time_after(jiffies, ieee->last_rx_ps_time + MSECS(timeout)))
1447                 return 0;
1448
1449         if ((ieee->softmac_features & IEEE_SOFTMAC_SINGLE_QUEUE) &&
1450                 (ieee->mgmt_queue_tail != ieee->mgmt_queue_head))
1451                 return 0;
1452
1453         if (time_l) {
1454                 *time_l = ieee->current_network.last_dtim_sta_time[0]
1455                         + MSECS((ieee->current_network.beacon_interval));
1456         }
1457
1458         if (time_h) {
1459                 *time_h = ieee->current_network.last_dtim_sta_time[1];
1460                 if (time_l && *time_l < ieee->current_network.last_dtim_sta_time[0])
1461                         *time_h += 1;
1462         }
1463
1464         return 1;
1465 }
1466
1467 static inline void ieee80211_sta_ps(struct ieee80211_device *ieee)
1468 {
1469
1470         u32 th, tl;
1471         short sleep;
1472
1473         unsigned long flags, flags2;
1474
1475         spin_lock_irqsave(&ieee->lock, flags);
1476
1477         if ((ieee->ps == IEEE80211_PS_DISABLED ||
1478                 ieee->iw_mode != IW_MODE_INFRA ||
1479                 ieee->state != IEEE80211_LINKED)) {
1480
1481                 /* #warning CHECK_LOCK_HERE */
1482                 spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
1483
1484                 ieee80211_sta_wakeup(ieee, 1);
1485
1486                 spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
1487         }
1488
1489         sleep = ieee80211_sta_ps_sleep(ieee, &th, &tl);
1490         /* 2 wake, 1 sleep, 0 do nothing */
1491         if (sleep == 0)
1492                 goto out;
1493
1494         if (sleep == 1) {
1495                 if (ieee->sta_sleep == 1)
1496                         ieee->enter_sleep_state(ieee->dev, th, tl);
1497
1498                 else if (ieee->sta_sleep == 0) {
1499                         spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
1500                         if (ieee->ps_is_queue_empty(ieee->dev)) {
1501                                 ieee->sta_sleep = 2;
1502
1503                                 ieee->ps_request_tx_ack(ieee->dev);
1504
1505                                 ieee80211_sta_ps_send_null_frame(ieee, 1);
1506
1507                                 ieee->ps_th = th;
1508                                 ieee->ps_tl = tl;
1509                         }
1510                         spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
1511                 }
1512         } else if (sleep == 2) {
1513                 /* #warning CHECK_LOCK_HERE */
1514                 spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
1515
1516                 ieee80211_sta_wakeup(ieee, 1);
1517
1518                 spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
1519         }
1520 out:
1521         spin_unlock_irqrestore(&ieee->lock, flags);
1522 }
1523
1524 void ieee80211_sta_wakeup(struct ieee80211_device *ieee, short nl)
1525 {
1526         if (ieee->sta_sleep == 0) {
1527                 if (nl) {
1528                         ieee->ps_request_tx_ack(ieee->dev);
1529                         ieee80211_sta_ps_send_null_frame(ieee, 0);
1530                 }
1531                 return;
1532         }
1533
1534         if (ieee->sta_sleep == 1)
1535                 ieee->sta_wake_up(ieee->dev);
1536
1537         ieee->sta_sleep = 0;
1538
1539         if (nl) {
1540                 ieee->ps_request_tx_ack(ieee->dev);
1541                 ieee80211_sta_ps_send_null_frame(ieee, 0);
1542         }
1543 }
1544
1545 void ieee80211_ps_tx_ack(struct ieee80211_device *ieee, short success)
1546 {
1547         unsigned long flags, flags2;
1548
1549         spin_lock_irqsave(&ieee->lock, flags);
1550         if (ieee->sta_sleep == 2) {
1551                 /* Null frame with PS bit set */
1552                 if (success) {
1553                         ieee->sta_sleep = 1;
1554                         ieee->enter_sleep_state(ieee->dev, ieee->ps_th, ieee->ps_tl);
1555                 }
1556                 /* if the card report not success we can't be sure the AP
1557                  * has not RXed so we can't assume the AP believe us awake
1558                  */
1559         } else {
1560                 if ((ieee->sta_sleep == 0) && !success) {
1561                         spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
1562                         ieee80211_sta_ps_send_null_frame(ieee, 0);
1563                         spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
1564                 }
1565         }
1566         spin_unlock_irqrestore(&ieee->lock, flags);
1567 }
1568
1569 inline int ieee80211_rx_frame_softmac(struct ieee80211_device *ieee,
1570                                       struct sk_buff *skb,
1571                                       struct ieee80211_rx_stats *rx_stats,
1572                                       u16 type, u16 stype)
1573 {
1574         struct ieee80211_hdr_3addr *header = (struct ieee80211_hdr_3addr *) skb->data;
1575         u16 errcode;
1576         u8 *challenge = NULL;
1577         int chlen = 0;
1578         int aid = 0;
1579         struct ieee80211_assoc_response_frame *assoc_resp;
1580         struct ieee80211_info_element *info_element;
1581
1582         if (!ieee->proto_started)
1583                 return 0;
1584
1585         if (ieee->sta_sleep || (ieee->ps != IEEE80211_PS_DISABLED &&
1586                 ieee->iw_mode == IW_MODE_INFRA &&
1587                 ieee->state == IEEE80211_LINKED))
1588
1589                 tasklet_schedule(&ieee->ps_task);
1590
1591         if (WLAN_FC_GET_STYPE(header->frame_control) != IEEE80211_STYPE_PROBE_RESP &&
1592                 WLAN_FC_GET_STYPE(header->frame_control) != IEEE80211_STYPE_BEACON)
1593                 ieee->last_rx_ps_time = jiffies;
1594
1595         switch (WLAN_FC_GET_STYPE(header->frame_control)) {
1596                 case IEEE80211_STYPE_ASSOC_RESP:
1597                 case IEEE80211_STYPE_REASSOC_RESP:
1598                         IEEE80211_DEBUG_MGMT("received [RE]ASSOCIATION RESPONSE (%d)\n",
1599                                         WLAN_FC_GET_STYPE(header->frame_ctl));
1600                         if ((ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) &&
1601                                 ieee->state == IEEE80211_ASSOCIATING_AUTHENTICATED &&
1602                                 ieee->iw_mode == IW_MODE_INFRA) {
1603                                 errcode = assoc_parse(skb, &aid);
1604                                 if (0 == errcode) {
1605                                         u16 left;
1606
1607                                         ieee->state = IEEE80211_LINKED;
1608                                         ieee->assoc_id = aid;
1609                                         ieee->softmac_stats.rx_ass_ok++;
1610                                         /* card type is 8187 */
1611                                         if (1 == rx_stats->nic_type)
1612                                                 goto associate_complete;
1613
1614                                         assoc_resp = (struct ieee80211_assoc_response_frame *)skb->data;
1615                                         info_element = &assoc_resp->info_element;
1616                                         left = skb->len - ((void *)info_element - (void *)assoc_resp);
1617
1618                                         while (left >= sizeof(struct ieee80211_info_element_hdr)) {
1619                                                 if (sizeof(struct ieee80211_info_element_hdr) + info_element->len > left) {
1620                                                         printk(KERN_WARNING "[re]associate response error!");
1621                                                         return 1;
1622                                                 }
1623                                                 switch (info_element->id) {
1624                                                   case MFIE_TYPE_GENERIC:
1625                                                         IEEE80211_DEBUG_SCAN("MFIE_TYPE_GENERIC: %d bytes\n", info_element->len);
1626                                                         if (info_element->len >= 8  &&
1627                                                             info_element->data[0] == 0x00 &&
1628                                                             info_element->data[1] == 0x50 &&
1629                                                             info_element->data[2] == 0xf2 &&
1630                                                             info_element->data[3] == 0x02 &&
1631                                                             info_element->data[4] == 0x01) {
1632                                                                 /* Not care about version at present.
1633                                                                  * WMM Parameter Element.
1634                                                                  */
1635                                                                 memcpy(ieee->current_network.wmm_param, (u8 *)(info_element->data\
1636                                                                         + 8), (info_element->len - 8));
1637
1638                                                                 if (((ieee->current_network.wmm_info^info_element->data[6])& \
1639                                                                                     0x0f) || (!ieee->init_wmmparam_flag)) {
1640                                                                         /* refresh parameter element for current network
1641                                                                          * update the register parameter for hardware.
1642                                                                          */
1643                                                                         ieee->init_wmmparam_flag = 1;
1644                                                                         queue_work(ieee->wq, &ieee->wmm_param_update_wq);
1645                                                                 }
1646                                                                 /* update info_element for current network */
1647                                                                 ieee->current_network.wmm_info  = info_element->data[6];
1648                                                         }
1649                                                         break;
1650                                                   default:
1651                                                         /* nothing to do at present!!! */
1652                                                         break;
1653                                                 }
1654
1655                                                 left -= sizeof(struct ieee80211_info_element_hdr) +
1656                                                         info_element->len;
1657                                                 info_element = (struct ieee80211_info_element *)
1658                                                         &info_element->data[info_element->len];
1659                                         }
1660                                         /* legacy AP, reset the AC_xx_param register */
1661                                         if (!ieee->init_wmmparam_flag) {
1662                                                 queue_work(ieee->wq, &ieee->wmm_param_update_wq);
1663                                                 ieee->init_wmmparam_flag = 1; /* indicate AC_xx_param upated since last associate */
1664                                         }
1665 associate_complete:
1666                                         ieee80211_associate_complete(ieee);
1667                                 } else {
1668                                         ieee->softmac_stats.rx_ass_err++;
1669                                         IEEE80211_DEBUG_MGMT(
1670                                                 "Association response status code 0x%x\n",
1671                                                 errcode);
1672                                         ieee80211_associate_abort(ieee);
1673                                 }
1674                         }
1675                         break;
1676                 case IEEE80211_STYPE_ASSOC_REQ:
1677                 case IEEE80211_STYPE_REASSOC_REQ:
1678                         if ((ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) &&
1679                                 ieee->iw_mode == IW_MODE_MASTER)
1680
1681                                 ieee80211_rx_assoc_rq(ieee, skb);
1682                         break;
1683                 case IEEE80211_STYPE_AUTH:
1684                         if (ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) {
1685                                 if (ieee->state == IEEE80211_ASSOCIATING_AUTHENTICATING &&
1686                                 ieee->iw_mode == IW_MODE_INFRA){
1687                                                 IEEE80211_DEBUG_MGMT("Received authentication response");
1688
1689                                                 errcode = auth_parse(skb, &challenge, &chlen);
1690                                                 if (0 == errcode) {
1691                                                         if (ieee->open_wep || !challenge) {
1692                                                                 ieee->state = IEEE80211_ASSOCIATING_AUTHENTICATED;
1693                                                                 ieee->softmac_stats.rx_auth_rs_ok++;
1694
1695                                                                 ieee80211_associate_step2(ieee);
1696                                                         } else {
1697                                                                 ieee80211_rtl_auth_challenge(ieee, challenge, chlen);
1698                                                         }
1699                                                 } else {
1700                                                         ieee->softmac_stats.rx_auth_rs_err++;
1701                                                         IEEE80211_DEBUG_MGMT("Authentication response status code 0x%x", errcode);
1702                                                         ieee80211_associate_abort(ieee);
1703                                                 }
1704
1705                                         } else if (ieee->iw_mode == IW_MODE_MASTER) {
1706                                                 ieee80211_rx_auth_rq(ieee, skb);
1707                                         }
1708                                 }
1709                         break;
1710                 case IEEE80211_STYPE_PROBE_REQ:
1711                         if ((ieee->softmac_features & IEEE_SOFTMAC_PROBERS) &&
1712                                 ((ieee->iw_mode == IW_MODE_ADHOC ||
1713                                 ieee->iw_mode == IW_MODE_MASTER) &&
1714                                 ieee->state == IEEE80211_LINKED))
1715
1716                                 ieee80211_rx_probe_rq(ieee, skb);
1717                         break;
1718                 case IEEE80211_STYPE_DISASSOC:
1719                 case IEEE80211_STYPE_DEAUTH:
1720                         /* FIXME for now repeat all the association procedure
1721                          * both for disassociation and deauthentication
1722                          */
1723                         if ((ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) &&
1724                                 (ieee->state == IEEE80211_LINKED) &&
1725                                 (ieee->iw_mode == IW_MODE_INFRA) &&
1726                                 (!memcmp(header->addr2, ieee->current_network.bssid, ETH_ALEN))) {
1727                                 ieee->state = IEEE80211_ASSOCIATING;
1728                                 ieee->softmac_stats.reassoc++;
1729
1730                                 queue_work(ieee->wq, &ieee->associate_procedure_wq);
1731                         }
1732                         break;
1733                 default:
1734                         return -1;
1735                         break;
1736         }
1737         return 0;
1738 }
1739
1740 /* following are for a simpler TX queue management.
1741  * Instead of using netif_[stop/wake]_queue the driver
1742  * will uses these two function (plus a reset one), that
1743  * will internally uses the kernel netif_* and takes
1744  * care of the ieee802.11 fragmentation.
1745  * So the driver receives a fragment per time and might
1746  * call the stop function when it want without take care
1747  * to have enough room to TX an entire packet.
1748  * This might be useful if each fragment need it's own
1749  * descriptor, thus just keep a total free memory > than
1750  * the max fragmentation threshold is not enough.. If the
1751  * ieee802.11 stack passed a TXB struct then you needed
1752  * to keep N free descriptors where
1753  * N = MAX_PACKET_SIZE / MIN_FRAG_TRESHOLD
1754  * In this way you need just one and the 802.11 stack
1755  * will take care of buffering fragments and pass them to
1756  * to the driver later, when it wakes the queue.
1757  */
1758
1759 void ieee80211_softmac_xmit(struct ieee80211_txb *txb,
1760                             struct ieee80211_device *ieee)
1761 {
1762         unsigned long flags;
1763         int  i;
1764
1765         spin_lock_irqsave(&ieee->lock, flags);
1766
1767         /* called with 2nd parm 0, no tx mgmt lock required */
1768         ieee80211_sta_wakeup(ieee, 0);
1769
1770         for (i = 0; i < txb->nr_frags; i++) {
1771                 if (ieee->queue_stop) {
1772                         ieee->tx_pending.txb = txb;
1773                         ieee->tx_pending.frag = i;
1774                         goto exit;
1775                 } else {
1776                         ieee->softmac_data_hard_start_xmit(
1777                                 txb->fragments[i],
1778                                 ieee->dev, ieee->rate);
1779                         ieee->stats.tx_packets++;
1780                         ieee->stats.tx_bytes += txb->fragments[i]->len;
1781                         ieee->dev->trans_start = jiffies;
1782                 }
1783         }
1784
1785         ieee80211_txb_free(txb);
1786
1787         exit:
1788         spin_unlock_irqrestore(&ieee->lock, flags);
1789 }
1790
1791 /* called with ieee->lock acquired */
1792 static void ieee80211_resume_tx(struct ieee80211_device *ieee)
1793 {
1794         int i;
1795         for (i = ieee->tx_pending.frag; i < ieee->tx_pending.txb->nr_frags; i++) {
1796
1797                 if (ieee->queue_stop) {
1798                         ieee->tx_pending.frag = i;
1799                         return;
1800                 } else {
1801                         ieee->softmac_data_hard_start_xmit(
1802                                 ieee->tx_pending.txb->fragments[i],
1803                                 ieee->dev, ieee->rate);
1804                         ieee->stats.tx_packets++;
1805                         ieee->dev->trans_start = jiffies;
1806                 }
1807         }
1808
1809         ieee80211_txb_free(ieee->tx_pending.txb);
1810         ieee->tx_pending.txb = NULL;
1811 }
1812
1813 void ieee80211_reset_queue(struct ieee80211_device *ieee)
1814 {
1815         unsigned long flags;
1816
1817         spin_lock_irqsave(&ieee->lock, flags);
1818         init_mgmt_queue(ieee);
1819         if (ieee->tx_pending.txb) {
1820                 ieee80211_txb_free(ieee->tx_pending.txb);
1821                 ieee->tx_pending.txb = NULL;
1822         }
1823         ieee->queue_stop = 0;
1824         spin_unlock_irqrestore(&ieee->lock, flags);
1825 }
1826
1827 void ieee80211_rtl_wake_queue(struct ieee80211_device *ieee)
1828 {
1829         unsigned long flags;
1830         struct sk_buff *skb;
1831         struct ieee80211_hdr_3addr  *header;
1832
1833         spin_lock_irqsave(&ieee->lock, flags);
1834         if (!ieee->queue_stop)
1835                 goto exit;
1836
1837         ieee->queue_stop = 0;
1838
1839         if (ieee->softmac_features & IEEE_SOFTMAC_SINGLE_QUEUE) {
1840                 while (!ieee->queue_stop && (skb = dequeue_mgmt(ieee))) {
1841                         header = (struct ieee80211_hdr_3addr  *) skb->data;
1842
1843                         header->seq_ctrl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
1844
1845                         if (ieee->seq_ctrl[0] == 0xFFF)
1846                                 ieee->seq_ctrl[0] = 0;
1847                         else
1848                                 ieee->seq_ctrl[0]++;
1849
1850                         ieee->softmac_data_hard_start_xmit(skb, ieee->dev, ieee->basic_rate);
1851                         dev_kfree_skb_any(skb);
1852                 }
1853         }
1854         if (!ieee->queue_stop && ieee->tx_pending.txb)
1855                 ieee80211_resume_tx(ieee);
1856
1857         if (!ieee->queue_stop && netif_queue_stopped(ieee->dev)) {
1858                 ieee->softmac_stats.swtxawake++;
1859                 netif_wake_queue(ieee->dev);
1860         }
1861 exit:
1862         spin_unlock_irqrestore(&ieee->lock, flags);
1863 }
1864
1865 void ieee80211_rtl_stop_queue(struct ieee80211_device *ieee)
1866 {
1867         if (!netif_queue_stopped(ieee->dev)) {
1868                 netif_stop_queue(ieee->dev);
1869                 ieee->softmac_stats.swtxstop++;
1870         }
1871         ieee->queue_stop = 1;
1872 }
1873
1874 inline void ieee80211_randomize_cell(struct ieee80211_device *ieee)
1875 {
1876         random_ether_addr(ieee->current_network.bssid);
1877 }
1878
1879 /* called in user context only */
1880 void ieee80211_start_master_bss(struct ieee80211_device *ieee)
1881 {
1882         ieee->assoc_id = 1;
1883
1884         if (ieee->current_network.ssid_len == 0) {
1885                 strncpy(ieee->current_network.ssid,
1886                         IEEE80211_DEFAULT_TX_ESSID,
1887                         IW_ESSID_MAX_SIZE);
1888
1889                 ieee->current_network.ssid_len = strlen(IEEE80211_DEFAULT_TX_ESSID);
1890                 ieee->ssid_set = 1;
1891         }
1892
1893         memcpy(ieee->current_network.bssid, ieee->dev->dev_addr, ETH_ALEN);
1894
1895         ieee->set_chan(ieee->dev, ieee->current_network.channel);
1896         ieee->state = IEEE80211_LINKED;
1897         ieee->link_change(ieee->dev);
1898         notify_wx_assoc_event(ieee);
1899
1900         if (ieee->data_hard_resume)
1901                 ieee->data_hard_resume(ieee->dev);
1902
1903         netif_carrier_on(ieee->dev);
1904 }
1905
1906 static void ieee80211_start_monitor_mode(struct ieee80211_device *ieee)
1907 {
1908         if (ieee->raw_tx) {
1909
1910                 if (ieee->data_hard_resume)
1911                         ieee->data_hard_resume(ieee->dev);
1912
1913                 netif_carrier_on(ieee->dev);
1914         }
1915 }
1916
1917 static void ieee80211_start_ibss_wq(struct work_struct *work)
1918 {
1919         struct delayed_work *dwork = to_delayed_work(work);
1920         struct ieee80211_device *ieee = container_of(dwork, struct ieee80211_device, start_ibss_wq);
1921
1922         /* iwconfig mode ad-hoc will schedule this and return
1923          * on the other hand this will block further iwconfig SET
1924          * operations because of the wx_sem hold.
1925          * Anyway some most set operations set a flag to speed-up
1926          * (abort) this wq (when syncro scanning) before sleeping
1927          * on the semaphore
1928          */
1929
1930         down(&ieee->wx_sem);
1931
1932         if (ieee->current_network.ssid_len == 0) {
1933                 strcpy(ieee->current_network.ssid, IEEE80211_DEFAULT_TX_ESSID);
1934                 ieee->current_network.ssid_len = strlen(IEEE80211_DEFAULT_TX_ESSID);
1935                 ieee->ssid_set = 1;
1936         }
1937
1938         /* check if we have this cell in our network list */
1939         ieee80211_softmac_check_all_nets(ieee);
1940
1941         if (ieee->state == IEEE80211_NOLINK)
1942                 ieee->current_network.channel = 10;
1943         /* if not then the state is not linked. Maybe the user switched to
1944          * ad-hoc mode just after being in monitor mode, or just after
1945          * being very few time in managed mode (so the card have had no
1946          * time to scan all the chans..) or we have just run up the iface
1947          * after setting ad-hoc mode. So we have to give another try..
1948          * Here, in ibss mode, should be safe to do this without extra care
1949          * (in bss mode we had to make sure no-one tried to associate when
1950          * we had just checked the ieee->state and we was going to start the
1951          * scan) because in ibss mode the ieee80211_new_net function, when
1952          * finds a good net, just set the ieee->state to IEEE80211_LINKED,
1953          * so, at worst, we waste a bit of time to initiate an unneeded syncro
1954          * scan, that will stop at the first round because it sees the state
1955          * associated.
1956          */
1957         if (ieee->state == IEEE80211_NOLINK)
1958                 ieee80211_start_scan_syncro(ieee);
1959
1960         /* the network definitively is not here.. create a new cell */
1961         if (ieee->state == IEEE80211_NOLINK) {
1962                 printk("creating new IBSS cell\n");
1963                 if (!ieee->wap_set)
1964                         ieee80211_randomize_cell(ieee);
1965
1966                 if (ieee->modulation & IEEE80211_CCK_MODULATION) {
1967                         ieee->current_network.rates_len = 4;
1968
1969                         ieee->current_network.rates[0] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_1MB;
1970                         ieee->current_network.rates[1] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_2MB;
1971                         ieee->current_network.rates[2] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_5MB;
1972                         ieee->current_network.rates[3] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_11MB;
1973
1974                 } else
1975                         ieee->current_network.rates_len = 0;
1976
1977                 if (ieee->modulation & IEEE80211_OFDM_MODULATION) {
1978                         ieee->current_network.rates_ex_len = 8;
1979
1980                         ieee->current_network.rates_ex[0] = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_6MB;
1981                         ieee->current_network.rates_ex[1] = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_9MB;
1982                         ieee->current_network.rates_ex[2] = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_12MB;
1983                         ieee->current_network.rates_ex[3] = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_18MB;
1984                         ieee->current_network.rates_ex[4] = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_24MB;
1985                         ieee->current_network.rates_ex[5] = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_36MB;
1986                         ieee->current_network.rates_ex[6] = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_48MB;
1987                         ieee->current_network.rates_ex[7] = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_54MB;
1988
1989                         ieee->rate = 540;
1990                 } else {
1991                         ieee->current_network.rates_ex_len = 0;
1992                         ieee->rate = 110;
1993                 }
1994
1995                 /* By default, WMM function will be disabled in IBSS mode */
1996                 ieee->current_network.QoS_Enable = 0;
1997
1998                 ieee->current_network.atim_window = 0;
1999                 ieee->current_network.capability = WLAN_CAPABILITY_IBSS;
2000                 if (ieee->short_slot)
2001                         ieee->current_network.capability |= WLAN_CAPABILITY_SHORT_SLOT;
2002         }
2003
2004         ieee->state = IEEE80211_LINKED;
2005         ieee->set_chan(ieee->dev, ieee->current_network.channel);
2006         ieee->link_change(ieee->dev);
2007
2008         notify_wx_assoc_event(ieee);
2009
2010         ieee80211_start_send_beacons(ieee);
2011         printk(KERN_WARNING "after sending beacon packet!\n");
2012
2013         if (ieee->data_hard_resume)
2014                 ieee->data_hard_resume(ieee->dev);
2015
2016         netif_carrier_on(ieee->dev);
2017
2018         up(&ieee->wx_sem);
2019 }
2020
2021 inline void ieee80211_start_ibss(struct ieee80211_device *ieee)
2022 {
2023         queue_delayed_work(ieee->wq, &ieee->start_ibss_wq, 100);
2024 }
2025
2026 /* this is called only in user context, with wx_sem held */
2027 void ieee80211_start_bss(struct ieee80211_device *ieee)
2028 {
2029         unsigned long flags;
2030         /* Ref: 802.11d 11.1.3.3
2031          * STA shall not start a BSS unless properly formed Beacon frame
2032          * including a Country IE.
2033          */
2034         if (IS_DOT11D_ENABLE(ieee) && !IS_COUNTRY_IE_VALID(ieee)) {
2035                 if (!ieee->bGlobalDomain)
2036                         return;
2037         }
2038         /* check if we have already found the net we are interested in (if any).
2039          * if not (we are disassociated and we are not
2040          * in associating / authenticating phase) start the background scanning.
2041          */
2042         ieee80211_softmac_check_all_nets(ieee);
2043
2044         /* ensure no-one start an associating process (thus setting
2045          * the ieee->state to ieee80211_ASSOCIATING) while we
2046          * have just cheked it and we are going to enable scan.
2047          * The ieee80211_new_net function is always called with
2048          * lock held (from both ieee80211_softmac_check_all_nets and
2049          * the rx path), so we cannot be in the middle of such function
2050          */
2051         spin_lock_irqsave(&ieee->lock, flags);
2052
2053         if (ieee->state == IEEE80211_NOLINK) {
2054                 ieee->actscanning = true;
2055                 ieee80211_rtl_start_scan(ieee);
2056         }
2057         spin_unlock_irqrestore(&ieee->lock, flags);
2058 }
2059
2060 /* called only in userspace context */
2061 void ieee80211_disassociate(struct ieee80211_device *ieee)
2062 {
2063         netif_carrier_off(ieee->dev);
2064
2065         if (ieee->softmac_features & IEEE_SOFTMAC_TX_QUEUE)
2066                         ieee80211_reset_queue(ieee);
2067
2068         if (ieee->data_hard_stop)
2069                         ieee->data_hard_stop(ieee->dev);
2070
2071         if (IS_DOT11D_ENABLE(ieee))
2072                 Dot11d_Reset(ieee);
2073
2074         ieee->link_change(ieee->dev);
2075         if (ieee->state == IEEE80211_LINKED)
2076                 notify_wx_assoc_event(ieee);
2077         ieee->state = IEEE80211_NOLINK;
2078
2079 }
2080 static void ieee80211_associate_retry_wq(struct work_struct *work)
2081 {
2082         struct delayed_work *dwork = to_delayed_work(work);
2083         struct ieee80211_device *ieee = container_of(dwork, struct ieee80211_device, associate_retry_wq);
2084         unsigned long flags;
2085         down(&ieee->wx_sem);
2086         if (!ieee->proto_started)
2087                 goto exit;
2088         if (ieee->state != IEEE80211_ASSOCIATING_RETRY)
2089                 goto exit;
2090         /* until we do not set the state to IEEE80211_NOLINK
2091         * there are no possibility to have someone else trying
2092         * to start an association procedure (we get here with
2093         * ieee->state = IEEE80211_ASSOCIATING).
2094         * When we set the state to IEEE80211_NOLINK it is possible
2095         * that the RX path run an attempt to associate, but
2096         * both ieee80211_softmac_check_all_nets and the
2097         * RX path works with ieee->lock held so there are no
2098         * problems. If we are still disassociated then start a scan.
2099         * the lock here is necessary to ensure no one try to start
2100         * an association procedure when we have just checked the
2101         * state and we are going to start the scan.
2102         */
2103         ieee->state = IEEE80211_NOLINK;
2104         ieee->beinretry = true;
2105         ieee80211_softmac_check_all_nets(ieee);
2106
2107         spin_lock_irqsave(&ieee->lock, flags);
2108
2109         if (ieee->state == IEEE80211_NOLINK) {
2110                 ieee->beinretry = false;
2111                 ieee->actscanning = true;
2112                 ieee80211_rtl_start_scan(ieee);
2113         }
2114         if (ieee->state == IEEE80211_NOLINK)
2115                 notify_wx_assoc_event(ieee);
2116         spin_unlock_irqrestore(&ieee->lock, flags);
2117
2118 exit:
2119         up(&ieee->wx_sem);
2120 }
2121
2122 struct sk_buff *ieee80211_get_beacon_(struct ieee80211_device *ieee)
2123 {
2124         u8 broadcast_addr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
2125
2126         struct sk_buff *skb = NULL;
2127         struct ieee80211_probe_response *b;
2128
2129         skb = ieee80211_probe_resp(ieee, broadcast_addr);
2130         if (!skb)
2131                 return NULL;
2132
2133         b = (struct ieee80211_probe_response *) skb->data;
2134         b->header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_BEACON);
2135
2136         return skb;
2137 }
2138
2139 struct sk_buff *ieee80211_get_beacon(struct ieee80211_device *ieee)
2140 {
2141         struct sk_buff *skb;
2142         struct ieee80211_probe_response *b;
2143
2144         skb = ieee80211_get_beacon_(ieee);
2145         if (!skb)
2146                 return NULL;
2147
2148         b = (struct ieee80211_probe_response *) skb->data;
2149         b->header.seq_ctrl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
2150
2151         if (ieee->seq_ctrl[0] == 0xFFF)
2152                 ieee->seq_ctrl[0] = 0;
2153         else
2154                 ieee->seq_ctrl[0]++;
2155
2156         return skb;
2157 }
2158
2159 void ieee80211_softmac_stop_protocol(struct ieee80211_device *ieee)
2160 {
2161         ieee->sync_scan_hurryup = 1;
2162         down(&ieee->wx_sem);
2163         ieee80211_stop_protocol(ieee);
2164         up(&ieee->wx_sem);
2165 }
2166
2167 void ieee80211_stop_protocol(struct ieee80211_device *ieee)
2168 {
2169         if (!ieee->proto_started)
2170                 return;
2171
2172         ieee->proto_started = 0;
2173
2174         ieee80211_stop_send_beacons(ieee);
2175         if ((ieee->iw_mode == IW_MODE_INFRA) && (ieee->state == IEEE80211_LINKED))
2176                 SendDisassociation(ieee, NULL, WLAN_REASON_DISASSOC_STA_HAS_LEFT);
2177
2178         del_timer_sync(&ieee->associate_timer);
2179         cancel_delayed_work(&ieee->associate_retry_wq);
2180         cancel_delayed_work(&ieee->start_ibss_wq);
2181         ieee80211_stop_scan(ieee);
2182
2183         ieee80211_disassociate(ieee);
2184 }
2185
2186 void ieee80211_softmac_start_protocol(struct ieee80211_device *ieee)
2187 {
2188         ieee->sync_scan_hurryup = 0;
2189         down(&ieee->wx_sem);
2190         ieee80211_start_protocol(ieee);
2191         up(&ieee->wx_sem);
2192 }
2193
2194 void ieee80211_start_protocol(struct ieee80211_device *ieee)
2195 {
2196         short ch = 0;
2197         int i = 0;
2198
2199         if (ieee->proto_started)
2200                 return;
2201
2202         ieee->proto_started = 1;
2203
2204         if (ieee->current_network.channel == 0) {
2205                 do {
2206                         ch++;
2207                         if (ch > MAX_CHANNEL_NUMBER)
2208                                 return; /* no channel found */
2209
2210                 } while (!GET_DOT11D_INFO(ieee)->channel_map[ch]);
2211
2212                 ieee->current_network.channel = ch;
2213         }
2214
2215         if (ieee->current_network.beacon_interval == 0)
2216                 ieee->current_network.beacon_interval = 100;
2217         ieee->set_chan(ieee->dev, ieee->current_network.channel);
2218
2219         for (i = 0; i < 17; i++) {
2220                 ieee->last_rxseq_num[i] = -1;
2221                 ieee->last_rxfrag_num[i] = -1;
2222                 ieee->last_packet_time[i] = 0;
2223         }
2224
2225         ieee->init_wmmparam_flag = 0; /* reinitialize AC_xx_PARAM registers. */
2226
2227         /* if the user set the MAC of the ad-hoc cell and then
2228          * switch to managed mode, shall we  make sure that association
2229          * attempts does not fail just because the user provide the essid
2230          * and the nic is still checking for the AP MAC ??
2231          */
2232         switch (ieee->iw_mode) {
2233                 case IW_MODE_AUTO:
2234                         ieee->iw_mode = IW_MODE_INFRA;
2235                         /* not set break here intentionly */
2236                 case IW_MODE_INFRA:
2237                         ieee80211_start_bss(ieee);
2238                         break;
2239
2240                 case IW_MODE_ADHOC:
2241                         ieee80211_start_ibss(ieee);
2242                         break;
2243
2244                 case IW_MODE_MASTER:
2245                         ieee80211_start_master_bss(ieee);
2246                 break;
2247
2248                 case IW_MODE_MONITOR:
2249                         ieee80211_start_monitor_mode(ieee);
2250                         break;
2251
2252                 default:
2253                         ieee->iw_mode = IW_MODE_INFRA;
2254                         ieee80211_start_bss(ieee);
2255                         break;
2256         }
2257 }
2258
2259 #define DRV_NAME  "Ieee80211"
2260 void ieee80211_softmac_init(struct ieee80211_device *ieee)
2261 {
2262         int i;
2263         memset(&ieee->current_network, 0, sizeof(struct ieee80211_network));
2264
2265         ieee->state = IEEE80211_NOLINK;
2266         ieee->sync_scan_hurryup = 0;
2267         for (i = 0; i < 5; i++)
2268                 ieee->seq_ctrl[i] = 0;
2269
2270         ieee->assoc_id = 0;
2271         ieee->queue_stop = 0;
2272         ieee->scanning = 0;
2273         ieee->softmac_features = 0; /* so IEEE2100-like driver are happy */
2274         ieee->wap_set = 0;
2275         ieee->ssid_set = 0;
2276         ieee->proto_started = 0;
2277         ieee->basic_rate = IEEE80211_DEFAULT_BASIC_RATE;
2278         ieee->rate = 3;
2279         ieee->ps = IEEE80211_PS_MBCAST|IEEE80211_PS_UNICAST;
2280         ieee->sta_sleep = 0;
2281         ieee->bInactivePs = false;
2282         ieee->actscanning = false;
2283         ieee->ListenInterval = 2;
2284         ieee->NumRxDataInPeriod = 0;
2285         ieee->NumRxBcnInPeriod = 0;
2286         ieee->NumRxOkTotal = 0;
2287         ieee->NumRxUnicast = 0; /* for keep alive */
2288         ieee->beinretry = false;
2289         ieee->bHwRadioOff = false;
2290
2291         init_mgmt_queue(ieee);
2292
2293         ieee->tx_pending.txb = NULL;
2294
2295         init_timer(&ieee->associate_timer);
2296         ieee->associate_timer.data = (unsigned long)ieee;
2297         ieee->associate_timer.function = ieee80211_associate_abort_cb;
2298
2299         init_timer(&ieee->beacon_timer);
2300         ieee->beacon_timer.data = (unsigned long) ieee;
2301         ieee->beacon_timer.function = ieee80211_send_beacon_cb;
2302
2303         ieee->wq = create_workqueue(DRV_NAME);
2304
2305         INIT_DELAYED_WORK(&ieee->start_ibss_wq, (void *) ieee80211_start_ibss_wq);
2306         INIT_WORK(&ieee->associate_complete_wq, (void *) ieee80211_associate_complete_wq);
2307         INIT_WORK(&ieee->associate_procedure_wq, (void *) ieee80211_associate_procedure_wq);
2308         INIT_DELAYED_WORK(&ieee->softmac_scan_wq, (void *) ieee80211_softmac_scan_wq);
2309         INIT_DELAYED_WORK(&ieee->associate_retry_wq, (void *) ieee80211_associate_retry_wq);
2310         INIT_WORK(&ieee->wx_sync_scan_wq, (void *) ieee80211_wx_sync_scan_wq);
2311
2312         sema_init(&ieee->wx_sem, 1);
2313         sema_init(&ieee->scan_sem, 1);
2314
2315         spin_lock_init(&ieee->mgmt_tx_lock);
2316         spin_lock_init(&ieee->beacon_lock);
2317
2318         tasklet_init(&ieee->ps_task,
2319              (void(*)(unsigned long)) ieee80211_sta_ps,
2320              (unsigned long)ieee);
2321         ieee->pDot11dInfo = kmalloc(sizeof(RT_DOT11D_INFO), GFP_ATOMIC);
2322 }
2323
2324 void ieee80211_softmac_free(struct ieee80211_device *ieee)
2325 {
2326         down(&ieee->wx_sem);
2327
2328         del_timer_sync(&ieee->associate_timer);
2329         cancel_delayed_work(&ieee->associate_retry_wq);
2330
2331         /* add for RF power on power of */
2332         cancel_delayed_work(&ieee->GPIOChangeRFWorkItem);
2333
2334         destroy_workqueue(ieee->wq);
2335         kfree(ieee->pDot11dInfo);
2336         up(&ieee->wx_sem);
2337 }
2338
2339 /* Start of WPA code. This is stolen from the ipw2200 driver  */
2340 static int ieee80211_wpa_enable(struct ieee80211_device *ieee, int value)
2341 {
2342         /* This is called when wpa_supplicant loads and closes the driver
2343          * interface. */
2344         printk("%s WPA\n", value ? "enabling" : "disabling");
2345         ieee->wpa_enabled = value;
2346         return 0;
2347 }
2348
2349 static void ieee80211_wpa_assoc_frame(struct ieee80211_device *ieee, char *wpa_ie,
2350                                int wpa_ie_len)
2351 {
2352         /* make sure WPA is enabled */
2353         ieee80211_wpa_enable(ieee, 1);
2354
2355         ieee80211_disassociate(ieee);
2356 }
2357
2358 static int ieee80211_wpa_mlme(struct ieee80211_device *ieee, int command,
2359                               int reason)
2360 {
2361         int ret = 0;
2362
2363         switch (command) {
2364         case IEEE_MLME_STA_DEAUTH:
2365                 /* silently ignore */
2366                 break;
2367
2368         case IEEE_MLME_STA_DISASSOC:
2369                 ieee80211_disassociate(ieee);
2370                 break;
2371
2372         default:
2373                 printk("Unknown MLME request: %d\n", command);
2374                 ret = -EOPNOTSUPP;
2375         }
2376
2377         return ret;
2378 }
2379
2380 static int ieee80211_wpa_set_wpa_ie(struct ieee80211_device *ieee,
2381                                     struct ieee_param *param, int plen)
2382 {
2383         u8 *buf;
2384
2385         if (param->u.wpa_ie.len > MAX_WPA_IE_LEN ||
2386             (param->u.wpa_ie.len && param->u.wpa_ie.data == NULL))
2387                 return -EINVAL;
2388
2389         if (param->u.wpa_ie.len) {
2390                 buf = kmemdup(param->u.wpa_ie.data, param->u.wpa_ie.len,
2391                               GFP_KERNEL);
2392                 if (buf == NULL)
2393                         return -ENOMEM;
2394
2395                 kfree(ieee->wpa_ie);
2396                 ieee->wpa_ie = buf;
2397                 ieee->wpa_ie_len = param->u.wpa_ie.len;
2398         } else {
2399                 kfree(ieee->wpa_ie);
2400                 ieee->wpa_ie = NULL;
2401                 ieee->wpa_ie_len = 0;
2402         }
2403
2404         ieee80211_wpa_assoc_frame(ieee, ieee->wpa_ie, ieee->wpa_ie_len);
2405         return 0;
2406 }
2407
2408 #define AUTH_ALG_OPEN_SYSTEM                    0x1
2409 #define AUTH_ALG_SHARED_KEY                     0x2
2410
2411 static int ieee80211_wpa_set_auth_algs(struct ieee80211_device *ieee, int value)
2412 {
2413         struct ieee80211_security sec = {
2414                 .flags = SEC_AUTH_MODE,
2415         };
2416         int ret = 0;
2417
2418         if (value & AUTH_ALG_SHARED_KEY) {
2419                 sec.auth_mode = WLAN_AUTH_SHARED_KEY;
2420                 ieee->open_wep = 0;
2421         } else {
2422                 sec.auth_mode = WLAN_AUTH_OPEN;
2423                 ieee->open_wep = 1;
2424         }
2425
2426         if (ieee->set_security)
2427                 ieee->set_security(ieee->dev, &sec);
2428         else
2429                 ret = -EOPNOTSUPP;
2430
2431         return ret;
2432 }
2433
2434 static int ieee80211_wpa_set_param(struct ieee80211_device *ieee, u8 name,
2435                                    u32 value)
2436 {
2437         int ret = 0;
2438         unsigned long flags;
2439
2440         switch (name) {
2441         case IEEE_PARAM_WPA_ENABLED:
2442                 ret = ieee80211_wpa_enable(ieee, value);
2443                 break;
2444
2445         case IEEE_PARAM_TKIP_COUNTERMEASURES:
2446                 ieee->tkip_countermeasures = value;
2447                 break;
2448
2449         case IEEE_PARAM_DROP_UNENCRYPTED: {
2450                 /* HACK:
2451                  *
2452                  * wpa_supplicant calls set_wpa_enabled when the driver
2453                  * is loaded and unloaded, regardless of if WPA is being
2454                  * used.  No other calls are made which can be used to
2455                  * determine if encryption will be used or not prior to
2456                  * association being expected.  If encryption is not being
2457                  * used, drop_unencrypted is set to false, else true -- we
2458                  * can use this to determine if the CAP_PRIVACY_ON bit should
2459                  * be set.
2460                  */
2461                 struct ieee80211_security sec = {
2462                         .flags = SEC_ENABLED,
2463                         .enabled = value,
2464                 };
2465                 ieee->drop_unencrypted = value;
2466                 /* We only change SEC_LEVEL for open mode. Others
2467                  * are set by ipw_wpa_set_encryption.
2468                  */
2469                 if (!value) {
2470                         sec.flags |= SEC_LEVEL;
2471                         sec.level = SEC_LEVEL_0;
2472                 } else {
2473                         sec.flags |= SEC_LEVEL;
2474                         sec.level = SEC_LEVEL_1;
2475                 }
2476                 if (ieee->set_security)
2477                         ieee->set_security(ieee->dev, &sec);
2478                 break;
2479         }
2480
2481         case IEEE_PARAM_PRIVACY_INVOKED:
2482                 ieee->privacy_invoked = value;
2483                 break;
2484         case IEEE_PARAM_AUTH_ALGS:
2485                 ret = ieee80211_wpa_set_auth_algs(ieee, value);
2486                 break;
2487         case IEEE_PARAM_IEEE_802_1X:
2488                 ieee->ieee802_1x = value;
2489                 break;
2490         case IEEE_PARAM_WPAX_SELECT:
2491                 spin_lock_irqsave(&ieee->wpax_suitlist_lock, flags);
2492                 ieee->wpax_type_set = 1;
2493                 ieee->wpax_type_notify = value;
2494                 spin_unlock_irqrestore(&ieee->wpax_suitlist_lock, flags);
2495                 break;
2496         default:
2497                 printk("Unknown WPA param: %d\n", name);
2498                 ret = -EOPNOTSUPP;
2499         }
2500
2501         return ret;
2502 }
2503
2504 /* implementation borrowed from hostap driver */
2505
2506 static int ieee80211_wpa_set_encryption(struct ieee80211_device *ieee,
2507                                         struct ieee_param *param, int param_len)
2508 {
2509         int ret = 0;
2510
2511         struct ieee80211_crypto_ops *ops;
2512         struct ieee80211_crypt_data **crypt;
2513
2514         struct ieee80211_security sec = {
2515                 .flags = 0,
2516         };
2517
2518         param->u.crypt.err = 0;
2519         param->u.crypt.alg[IEEE_CRYPT_ALG_NAME_LEN - 1] = '\0';
2520
2521         if (param_len !=
2522             (int) ((char *) param->u.crypt.key - (char *) param) +
2523             param->u.crypt.key_len) {
2524                 printk("Len mismatch %d, %d\n", param_len,
2525                                param->u.crypt.key_len);
2526                 return -EINVAL;
2527         }
2528         if (is_broadcast_ether_addr(param->sta_addr)) {
2529                 if (param->u.crypt.idx >= WEP_KEYS)
2530                         return -EINVAL;
2531                 crypt = &ieee->crypt[param->u.crypt.idx];
2532         } else {
2533                 return -EINVAL;
2534         }
2535
2536         if (strcmp(param->u.crypt.alg, "none") == 0) {
2537                 if (crypt) {
2538                         sec.enabled = 0;
2539                         /* FIXME FIXME */
2540                         sec.level = SEC_LEVEL_0;
2541                         sec.flags |= SEC_ENABLED | SEC_LEVEL;
2542                         ieee80211_crypt_delayed_deinit(ieee, crypt);
2543                 }
2544                 goto done;
2545         }
2546         sec.enabled = 1;
2547         /* FIXME FIXME */
2548         sec.flags |= SEC_ENABLED;
2549
2550         /* IPW HW cannot build TKIP MIC, host decryption still needed. */
2551         if (!(ieee->host_encrypt || ieee->host_decrypt) &&
2552             strcmp(param->u.crypt.alg, "TKIP"))
2553                 goto skip_host_crypt;
2554
2555         ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
2556         if (ops == NULL && strcmp(param->u.crypt.alg, "WEP") == 0)
2557                 ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
2558         else if (ops == NULL && strcmp(param->u.crypt.alg, "TKIP") == 0)
2559                 ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
2560         else if (ops == NULL && strcmp(param->u.crypt.alg, "CCMP") == 0)
2561                 ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
2562         if (ops == NULL) {
2563                 printk("unknown crypto alg '%s'\n", param->u.crypt.alg);
2564                 param->u.crypt.err = IEEE_CRYPT_ERR_UNKNOWN_ALG;
2565                 ret = -EINVAL;
2566                 goto done;
2567         }
2568
2569         if (*crypt == NULL || (*crypt)->ops != ops) {
2570                 struct ieee80211_crypt_data *new_crypt;
2571
2572                 ieee80211_crypt_delayed_deinit(ieee, crypt);
2573
2574                 new_crypt = kmalloc(sizeof(*new_crypt), GFP_KERNEL);
2575                 if (new_crypt == NULL) {
2576                         ret = -ENOMEM;
2577                         goto done;
2578                 }
2579                 memset(new_crypt, 0, sizeof(struct ieee80211_crypt_data));
2580                 new_crypt->ops = ops;
2581                 if (new_crypt->ops)
2582                         new_crypt->priv =
2583                                 new_crypt->ops->init(param->u.crypt.idx);
2584
2585                 if (new_crypt->priv == NULL) {
2586                         kfree(new_crypt);
2587                         param->u.crypt.err = IEEE_CRYPT_ERR_CRYPT_INIT_FAILED;
2588                         ret = -EINVAL;
2589                         goto done;
2590                 }
2591
2592                 *crypt = new_crypt;
2593         }
2594
2595         if (param->u.crypt.key_len > 0 && (*crypt)->ops->set_key &&
2596             (*crypt)->ops->set_key(param->u.crypt.key,
2597                                    param->u.crypt.key_len, param->u.crypt.seq,
2598                                    (*crypt)->priv) < 0) {
2599                 printk("key setting failed\n");
2600                 param->u.crypt.err = IEEE_CRYPT_ERR_KEY_SET_FAILED;
2601                 ret = -EINVAL;
2602                 goto done;
2603         }
2604
2605  skip_host_crypt:
2606         if (param->u.crypt.set_tx) {
2607                 ieee->tx_keyidx = param->u.crypt.idx;
2608                 sec.active_key = param->u.crypt.idx;
2609                 sec.flags |= SEC_ACTIVE_KEY;
2610         } else
2611                 sec.flags &= ~SEC_ACTIVE_KEY;
2612
2613         if (param->u.crypt.alg != NULL) {
2614                 memcpy(sec.keys[param->u.crypt.idx],
2615                        param->u.crypt.key,
2616                        param->u.crypt.key_len);
2617                 sec.key_sizes[param->u.crypt.idx] = param->u.crypt.key_len;
2618                 sec.flags |= (1 << param->u.crypt.idx);
2619
2620                 if (strcmp(param->u.crypt.alg, "WEP") == 0) {
2621                         sec.flags |= SEC_LEVEL;
2622                         sec.level = SEC_LEVEL_1;
2623                 } else if (strcmp(param->u.crypt.alg, "TKIP") == 0) {
2624                         sec.flags |= SEC_LEVEL;
2625                         sec.level = SEC_LEVEL_2;
2626                 } else if (strcmp(param->u.crypt.alg, "CCMP") == 0) {
2627                         sec.flags |= SEC_LEVEL;
2628                         sec.level = SEC_LEVEL_3;
2629                 }
2630         }
2631  done:
2632         if (ieee->set_security)
2633                 ieee->set_security(ieee->dev, &sec);
2634
2635         /* Do not reset port if card is in Managed mode since resetting will
2636          * generate new IEEE 802.11 authentication which may end up in looping
2637          * with IEEE 802.1X.  If your hardware requires a reset after WEP
2638          * configuration (for example... Prism2), implement the reset_port in
2639          * the callbacks structures used to initialize the 802.11 stack. */
2640         if (ieee->reset_on_keychange &&
2641             ieee->iw_mode != IW_MODE_INFRA &&
2642             ieee->reset_port &&
2643             ieee->reset_port(ieee->dev)) {
2644                 printk("reset_port failed\n");
2645                 param->u.crypt.err = IEEE_CRYPT_ERR_CARD_CONF_FAILED;
2646                 return -EINVAL;
2647         }
2648
2649         return ret;
2650 }
2651
2652 int ieee80211_wpa_supplicant_ioctl(struct ieee80211_device *ieee,
2653                                    struct iw_point *p)
2654 {
2655         struct ieee_param *param;
2656         int ret = 0;
2657
2658         down(&ieee->wx_sem);
2659
2660         if (p->length < sizeof(struct ieee_param) || !p->pointer) {
2661                 ret = -EINVAL;
2662                 goto out;
2663         }
2664
2665         param = memdup_user(p->pointer, p->length);
2666         if (IS_ERR(param)) {
2667                 ret = PTR_ERR(param);
2668                 goto out;
2669         }
2670
2671         switch (param->cmd) {
2672         case IEEE_CMD_SET_WPA_PARAM:
2673                 ret = ieee80211_wpa_set_param(ieee, param->u.wpa_param.name,
2674                                         param->u.wpa_param.value);
2675                 break;
2676         case IEEE_CMD_SET_WPA_IE:
2677                 ret = ieee80211_wpa_set_wpa_ie(ieee, param, p->length);
2678                 break;
2679         case IEEE_CMD_SET_ENCRYPTION:
2680                 ret = ieee80211_wpa_set_encryption(ieee, param, p->length);
2681                 break;
2682         case IEEE_CMD_MLME:
2683                 ret = ieee80211_wpa_mlme(ieee, param->u.mlme.command,
2684                                    param->u.mlme.reason_code);
2685                 break;
2686         default:
2687                 printk("Unknown WPA supplicant request: %d\n", param->cmd);
2688                 ret = -EOPNOTSUPP;
2689                 break;
2690         }
2691
2692         if (ret == 0 && copy_to_user(p->pointer, param, p->length))
2693                 ret = -EFAULT;
2694
2695         kfree(param);
2696 out:
2697         up(&ieee->wx_sem);
2698
2699         return ret;
2700 }
2701
2702 void notify_wx_assoc_event(struct ieee80211_device *ieee)
2703 {
2704         union iwreq_data wrqu;
2705         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
2706         if (ieee->state == IEEE80211_LINKED)
2707                 memcpy(wrqu.ap_addr.sa_data, ieee->current_network.bssid, ETH_ALEN);
2708         else
2709                 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
2710         wireless_send_event(ieee->dev, SIOCGIWAP, &wrqu, NULL);
2711 }