proc: introduce proc_create_single{,_data}
[linux-block.git] / drivers / net / wireless / intersil / hostap / hostap_ap.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Intersil Prism2 driver with Host AP (software access point) support
4  * Copyright (c) 2001-2002, SSH Communications Security Corp and Jouni Malinen
5  * <j@w1.fi>
6  * Copyright (c) 2002-2005, Jouni Malinen <j@w1.fi>
7  *
8  * This file is to be included into hostap.c when S/W AP functionality is
9  * compiled.
10  *
11  * AP:  FIX:
12  * - if unicast Class 2 (assoc,reassoc,disassoc) frame received from
13  *   unauthenticated STA, send deauth. frame (8802.11: 5.5)
14  * - if unicast Class 3 (data with to/from DS,deauth,pspoll) frame received
15  *   from authenticated, but unassoc STA, send disassoc frame (8802.11: 5.5)
16  * - if unicast Class 3 received from unauthenticated STA, send deauth. frame
17  *   (8802.11: 5.5)
18  */
19
20 #include <linux/proc_fs.h>
21 #include <linux/seq_file.h>
22 #include <linux/delay.h>
23 #include <linux/random.h>
24 #include <linux/if_arp.h>
25 #include <linux/slab.h>
26 #include <linux/export.h>
27 #include <linux/moduleparam.h>
28 #include <linux/etherdevice.h>
29
30 #include "hostap_wlan.h"
31 #include "hostap.h"
32 #include "hostap_ap.h"
33
34 static int other_ap_policy[MAX_PARM_DEVICES] = { AP_OTHER_AP_SKIP_ALL,
35                                                  DEF_INTS };
36 module_param_array(other_ap_policy, int, NULL, 0444);
37 MODULE_PARM_DESC(other_ap_policy, "Other AP beacon monitoring policy (0-3)");
38
39 static int ap_max_inactivity[MAX_PARM_DEVICES] = { AP_MAX_INACTIVITY_SEC,
40                                                    DEF_INTS };
41 module_param_array(ap_max_inactivity, int, NULL, 0444);
42 MODULE_PARM_DESC(ap_max_inactivity, "AP timeout (in seconds) for station "
43                  "inactivity");
44
45 static int ap_bridge_packets[MAX_PARM_DEVICES] = { 1, DEF_INTS };
46 module_param_array(ap_bridge_packets, int, NULL, 0444);
47 MODULE_PARM_DESC(ap_bridge_packets, "Bridge packets directly between "
48                  "stations");
49
50 static int autom_ap_wds[MAX_PARM_DEVICES] = { 0, DEF_INTS };
51 module_param_array(autom_ap_wds, int, NULL, 0444);
52 MODULE_PARM_DESC(autom_ap_wds, "Add WDS connections to other APs "
53                  "automatically");
54
55
56 static struct sta_info* ap_get_sta(struct ap_data *ap, u8 *sta);
57 static void hostap_event_expired_sta(struct net_device *dev,
58                                      struct sta_info *sta);
59 static void handle_add_proc_queue(struct work_struct *work);
60
61 #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
62 static void handle_wds_oper_queue(struct work_struct *work);
63 static void prism2_send_mgmt(struct net_device *dev,
64                              u16 type_subtype, char *body,
65                              int body_len, u8 *addr, u16 tx_cb_idx);
66 #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
67
68
69 #ifndef PRISM2_NO_PROCFS_DEBUG
70 static int ap_debug_proc_show(struct seq_file *m, void *v)
71 {
72         struct ap_data *ap = m->private;
73
74         seq_printf(m, "BridgedUnicastFrames=%u\n", ap->bridged_unicast);
75         seq_printf(m, "BridgedMulticastFrames=%u\n", ap->bridged_multicast);
76         seq_printf(m, "max_inactivity=%u\n", ap->max_inactivity / HZ);
77         seq_printf(m, "bridge_packets=%u\n", ap->bridge_packets);
78         seq_printf(m, "nullfunc_ack=%u\n", ap->nullfunc_ack);
79         seq_printf(m, "autom_ap_wds=%u\n", ap->autom_ap_wds);
80         seq_printf(m, "auth_algs=%u\n", ap->local->auth_algs);
81         seq_printf(m, "tx_drop_nonassoc=%u\n", ap->tx_drop_nonassoc);
82         return 0;
83 }
84
85 static int ap_debug_proc_open(struct inode *inode, struct file *file)
86 {
87         return single_open(file, ap_debug_proc_show, PDE_DATA(inode));
88 }
89
90 static const struct file_operations ap_debug_proc_fops = {
91         .open           = ap_debug_proc_open,
92         .read           = seq_read,
93         .llseek         = seq_lseek,
94         .release        = single_release,
95 };
96 #endif /* PRISM2_NO_PROCFS_DEBUG */
97
98
99 static void ap_sta_hash_add(struct ap_data *ap, struct sta_info *sta)
100 {
101         sta->hnext = ap->sta_hash[STA_HASH(sta->addr)];
102         ap->sta_hash[STA_HASH(sta->addr)] = sta;
103 }
104
105 static void ap_sta_hash_del(struct ap_data *ap, struct sta_info *sta)
106 {
107         struct sta_info *s;
108
109         s = ap->sta_hash[STA_HASH(sta->addr)];
110         if (s == NULL) return;
111         if (ether_addr_equal(s->addr, sta->addr)) {
112                 ap->sta_hash[STA_HASH(sta->addr)] = s->hnext;
113                 return;
114         }
115
116         while (s->hnext != NULL && !ether_addr_equal(s->hnext->addr, sta->addr))
117                 s = s->hnext;
118         if (s->hnext != NULL)
119                 s->hnext = s->hnext->hnext;
120         else
121                 printk("AP: could not remove STA %pM from hash table\n",
122                        sta->addr);
123 }
124
125 static void ap_free_sta(struct ap_data *ap, struct sta_info *sta)
126 {
127         if (sta->ap && sta->local)
128                 hostap_event_expired_sta(sta->local->dev, sta);
129
130         if (ap->proc != NULL) {
131                 char name[20];
132                 sprintf(name, "%pM", sta->addr);
133                 remove_proc_entry(name, ap->proc);
134         }
135
136         if (sta->crypt) {
137                 sta->crypt->ops->deinit(sta->crypt->priv);
138                 kfree(sta->crypt);
139                 sta->crypt = NULL;
140         }
141
142         skb_queue_purge(&sta->tx_buf);
143
144         ap->num_sta--;
145 #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
146         if (sta->aid > 0)
147                 ap->sta_aid[sta->aid - 1] = NULL;
148
149         if (!sta->ap)
150                 kfree(sta->u.sta.challenge);
151         del_timer_sync(&sta->timer);
152 #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
153
154         kfree(sta);
155 }
156
157
158 static void hostap_set_tim(local_info_t *local, int aid, int set)
159 {
160         if (local->func->set_tim)
161                 local->func->set_tim(local->dev, aid, set);
162 }
163
164
165 static void hostap_event_new_sta(struct net_device *dev, struct sta_info *sta)
166 {
167         union iwreq_data wrqu;
168         memset(&wrqu, 0, sizeof(wrqu));
169         memcpy(wrqu.addr.sa_data, sta->addr, ETH_ALEN);
170         wrqu.addr.sa_family = ARPHRD_ETHER;
171         wireless_send_event(dev, IWEVREGISTERED, &wrqu, NULL);
172 }
173
174
175 static void hostap_event_expired_sta(struct net_device *dev,
176                                      struct sta_info *sta)
177 {
178         union iwreq_data wrqu;
179         memset(&wrqu, 0, sizeof(wrqu));
180         memcpy(wrqu.addr.sa_data, sta->addr, ETH_ALEN);
181         wrqu.addr.sa_family = ARPHRD_ETHER;
182         wireless_send_event(dev, IWEVEXPIRED, &wrqu, NULL);
183 }
184
185
186 #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
187
188 static void ap_handle_timer(struct timer_list *t)
189 {
190         struct sta_info *sta = from_timer(sta, t, timer);
191         local_info_t *local;
192         struct ap_data *ap;
193         unsigned long next_time = 0;
194         int was_assoc;
195
196         if (sta == NULL || sta->local == NULL || sta->local->ap == NULL) {
197                 PDEBUG(DEBUG_AP, "ap_handle_timer() called with NULL data\n");
198                 return;
199         }
200
201         local = sta->local;
202         ap = local->ap;
203         was_assoc = sta->flags & WLAN_STA_ASSOC;
204
205         if (atomic_read(&sta->users) != 0)
206                 next_time = jiffies + HZ;
207         else if ((sta->flags & WLAN_STA_PERM) && !(sta->flags & WLAN_STA_AUTH))
208                 next_time = jiffies + ap->max_inactivity;
209
210         if (time_before(jiffies, sta->last_rx + ap->max_inactivity)) {
211                 /* station activity detected; reset timeout state */
212                 sta->timeout_next = STA_NULLFUNC;
213                 next_time = sta->last_rx + ap->max_inactivity;
214         } else if (sta->timeout_next == STA_DISASSOC &&
215                    !(sta->flags & WLAN_STA_PENDING_POLL)) {
216                 /* STA ACKed data nullfunc frame poll */
217                 sta->timeout_next = STA_NULLFUNC;
218                 next_time = jiffies + ap->max_inactivity;
219         }
220
221         if (next_time) {
222                 sta->timer.expires = next_time;
223                 add_timer(&sta->timer);
224                 return;
225         }
226
227         if (sta->ap)
228                 sta->timeout_next = STA_DEAUTH;
229
230         if (sta->timeout_next == STA_DEAUTH && !(sta->flags & WLAN_STA_PERM)) {
231                 spin_lock(&ap->sta_table_lock);
232                 ap_sta_hash_del(ap, sta);
233                 list_del(&sta->list);
234                 spin_unlock(&ap->sta_table_lock);
235                 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
236         } else if (sta->timeout_next == STA_DISASSOC)
237                 sta->flags &= ~WLAN_STA_ASSOC;
238
239         if (was_assoc && !(sta->flags & WLAN_STA_ASSOC) && !sta->ap)
240                 hostap_event_expired_sta(local->dev, sta);
241
242         if (sta->timeout_next == STA_DEAUTH && sta->aid > 0 &&
243             !skb_queue_empty(&sta->tx_buf)) {
244                 hostap_set_tim(local, sta->aid, 0);
245                 sta->flags &= ~WLAN_STA_TIM;
246         }
247
248         if (sta->ap) {
249                 if (ap->autom_ap_wds) {
250                         PDEBUG(DEBUG_AP, "%s: removing automatic WDS "
251                                "connection to AP %pM\n",
252                                local->dev->name, sta->addr);
253                         hostap_wds_link_oper(local, sta->addr, WDS_DEL);
254                 }
255         } else if (sta->timeout_next == STA_NULLFUNC) {
256                 /* send data frame to poll STA and check whether this frame
257                  * is ACKed */
258                 /* FIX: IEEE80211_STYPE_NULLFUNC would be more appropriate, but
259                  * it is apparently not retried so TX Exc events are not
260                  * received for it */
261                 sta->flags |= WLAN_STA_PENDING_POLL;
262                 prism2_send_mgmt(local->dev, IEEE80211_FTYPE_DATA |
263                                  IEEE80211_STYPE_DATA, NULL, 0,
264                                  sta->addr, ap->tx_callback_poll);
265         } else {
266                 int deauth = sta->timeout_next == STA_DEAUTH;
267                 __le16 resp;
268                 PDEBUG(DEBUG_AP, "%s: sending %s info to STA %pM"
269                        "(last=%lu, jiffies=%lu)\n",
270                        local->dev->name,
271                        deauth ? "deauthentication" : "disassociation",
272                        sta->addr, sta->last_rx, jiffies);
273
274                 resp = cpu_to_le16(deauth ? WLAN_REASON_PREV_AUTH_NOT_VALID :
275                                    WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
276                 prism2_send_mgmt(local->dev, IEEE80211_FTYPE_MGMT |
277                                  (deauth ? IEEE80211_STYPE_DEAUTH :
278                                   IEEE80211_STYPE_DISASSOC),
279                                  (char *) &resp, 2, sta->addr, 0);
280         }
281
282         if (sta->timeout_next == STA_DEAUTH) {
283                 if (sta->flags & WLAN_STA_PERM) {
284                         PDEBUG(DEBUG_AP, "%s: STA %pM"
285                                " would have been removed, "
286                                "but it has 'perm' flag\n",
287                                local->dev->name, sta->addr);
288                 } else
289                         ap_free_sta(ap, sta);
290                 return;
291         }
292
293         if (sta->timeout_next == STA_NULLFUNC) {
294                 sta->timeout_next = STA_DISASSOC;
295                 sta->timer.expires = jiffies + AP_DISASSOC_DELAY;
296         } else {
297                 sta->timeout_next = STA_DEAUTH;
298                 sta->timer.expires = jiffies + AP_DEAUTH_DELAY;
299         }
300
301         add_timer(&sta->timer);
302 }
303
304
305 void hostap_deauth_all_stas(struct net_device *dev, struct ap_data *ap,
306                             int resend)
307 {
308         u8 addr[ETH_ALEN];
309         __le16 resp;
310         int i;
311
312         PDEBUG(DEBUG_AP, "%s: Deauthenticate all stations\n", dev->name);
313         eth_broadcast_addr(addr);
314
315         resp = cpu_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
316
317         /* deauth message sent; try to resend it few times; the message is
318          * broadcast, so it may be delayed until next DTIM; there is not much
319          * else we can do at this point since the driver is going to be shut
320          * down */
321         for (i = 0; i < 5; i++) {
322                 prism2_send_mgmt(dev, IEEE80211_FTYPE_MGMT |
323                                  IEEE80211_STYPE_DEAUTH,
324                                  (char *) &resp, 2, addr, 0);
325
326                 if (!resend || ap->num_sta <= 0)
327                         return;
328
329                 mdelay(50);
330         }
331 }
332
333
334 static int ap_control_proc_show(struct seq_file *m, void *v)
335 {
336         struct ap_data *ap = m->private;
337         char *policy_txt;
338         struct mac_entry *entry;
339
340         if (v == SEQ_START_TOKEN) {
341                 switch (ap->mac_restrictions.policy) {
342                 case MAC_POLICY_OPEN:
343                         policy_txt = "open";
344                         break;
345                 case MAC_POLICY_ALLOW:
346                         policy_txt = "allow";
347                         break;
348                 case MAC_POLICY_DENY:
349                         policy_txt = "deny";
350                         break;
351                 default:
352                         policy_txt = "unknown";
353                         break;
354                 }
355                 seq_printf(m, "MAC policy: %s\n", policy_txt);
356                 seq_printf(m, "MAC entries: %u\n", ap->mac_restrictions.entries);
357                 seq_puts(m, "MAC list:\n");
358                 return 0;
359         }
360
361         entry = v;
362         seq_printf(m, "%pM\n", entry->addr);
363         return 0;
364 }
365
366 static void *ap_control_proc_start(struct seq_file *m, loff_t *_pos)
367 {
368         struct ap_data *ap = m->private;
369         spin_lock_bh(&ap->mac_restrictions.lock);
370         return seq_list_start_head(&ap->mac_restrictions.mac_list, *_pos);
371 }
372
373 static void *ap_control_proc_next(struct seq_file *m, void *v, loff_t *_pos)
374 {
375         struct ap_data *ap = m->private;
376         return seq_list_next(v, &ap->mac_restrictions.mac_list, _pos);
377 }
378
379 static void ap_control_proc_stop(struct seq_file *m, void *v)
380 {
381         struct ap_data *ap = m->private;
382         spin_unlock_bh(&ap->mac_restrictions.lock);
383 }
384
385 static const struct seq_operations ap_control_proc_seqops = {
386         .start  = ap_control_proc_start,
387         .next   = ap_control_proc_next,
388         .stop   = ap_control_proc_stop,
389         .show   = ap_control_proc_show,
390 };
391
392 static int ap_control_proc_open(struct inode *inode, struct file *file)
393 {
394         int ret = seq_open(file, &ap_control_proc_seqops);
395         if (ret == 0) {
396                 struct seq_file *m = file->private_data;
397                 m->private = PDE_DATA(inode);
398         }
399         return ret;
400 }
401
402 static const struct file_operations ap_control_proc_fops = {
403         .open           = ap_control_proc_open,
404         .read           = seq_read,
405         .llseek         = seq_lseek,
406         .release        = seq_release,
407 };
408
409
410 int ap_control_add_mac(struct mac_restrictions *mac_restrictions, u8 *mac)
411 {
412         struct mac_entry *entry;
413
414         entry = kmalloc(sizeof(struct mac_entry), GFP_KERNEL);
415         if (entry == NULL)
416                 return -ENOMEM;
417
418         memcpy(entry->addr, mac, ETH_ALEN);
419
420         spin_lock_bh(&mac_restrictions->lock);
421         list_add_tail(&entry->list, &mac_restrictions->mac_list);
422         mac_restrictions->entries++;
423         spin_unlock_bh(&mac_restrictions->lock);
424
425         return 0;
426 }
427
428
429 int ap_control_del_mac(struct mac_restrictions *mac_restrictions, u8 *mac)
430 {
431         struct list_head *ptr;
432         struct mac_entry *entry;
433
434         spin_lock_bh(&mac_restrictions->lock);
435         for (ptr = mac_restrictions->mac_list.next;
436              ptr != &mac_restrictions->mac_list; ptr = ptr->next) {
437                 entry = list_entry(ptr, struct mac_entry, list);
438
439                 if (ether_addr_equal(entry->addr, mac)) {
440                         list_del(ptr);
441                         kfree(entry);
442                         mac_restrictions->entries--;
443                         spin_unlock_bh(&mac_restrictions->lock);
444                         return 0;
445                 }
446         }
447         spin_unlock_bh(&mac_restrictions->lock);
448         return -1;
449 }
450
451
452 static int ap_control_mac_deny(struct mac_restrictions *mac_restrictions,
453                                u8 *mac)
454 {
455         struct mac_entry *entry;
456         int found = 0;
457
458         if (mac_restrictions->policy == MAC_POLICY_OPEN)
459                 return 0;
460
461         spin_lock_bh(&mac_restrictions->lock);
462         list_for_each_entry(entry, &mac_restrictions->mac_list, list) {
463                 if (ether_addr_equal(entry->addr, mac)) {
464                         found = 1;
465                         break;
466                 }
467         }
468         spin_unlock_bh(&mac_restrictions->lock);
469
470         if (mac_restrictions->policy == MAC_POLICY_ALLOW)
471                 return !found;
472         else
473                 return found;
474 }
475
476
477 void ap_control_flush_macs(struct mac_restrictions *mac_restrictions)
478 {
479         struct list_head *ptr, *n;
480         struct mac_entry *entry;
481
482         if (mac_restrictions->entries == 0)
483                 return;
484
485         spin_lock_bh(&mac_restrictions->lock);
486         for (ptr = mac_restrictions->mac_list.next, n = ptr->next;
487              ptr != &mac_restrictions->mac_list;
488              ptr = n, n = ptr->next) {
489                 entry = list_entry(ptr, struct mac_entry, list);
490                 list_del(ptr);
491                 kfree(entry);
492         }
493         mac_restrictions->entries = 0;
494         spin_unlock_bh(&mac_restrictions->lock);
495 }
496
497
498 int ap_control_kick_mac(struct ap_data *ap, struct net_device *dev, u8 *mac)
499 {
500         struct sta_info *sta;
501         __le16 resp;
502
503         spin_lock_bh(&ap->sta_table_lock);
504         sta = ap_get_sta(ap, mac);
505         if (sta) {
506                 ap_sta_hash_del(ap, sta);
507                 list_del(&sta->list);
508         }
509         spin_unlock_bh(&ap->sta_table_lock);
510
511         if (!sta)
512                 return -EINVAL;
513
514         resp = cpu_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
515         prism2_send_mgmt(dev, IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_DEAUTH,
516                          (char *) &resp, 2, sta->addr, 0);
517
518         if ((sta->flags & WLAN_STA_ASSOC) && !sta->ap)
519                 hostap_event_expired_sta(dev, sta);
520
521         ap_free_sta(ap, sta);
522
523         return 0;
524 }
525
526 #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
527
528
529 void ap_control_kickall(struct ap_data *ap)
530 {
531         struct list_head *ptr, *n;
532         struct sta_info *sta;
533
534         spin_lock_bh(&ap->sta_table_lock);
535         for (ptr = ap->sta_list.next, n = ptr->next; ptr != &ap->sta_list;
536              ptr = n, n = ptr->next) {
537                 sta = list_entry(ptr, struct sta_info, list);
538                 ap_sta_hash_del(ap, sta);
539                 list_del(&sta->list);
540                 if ((sta->flags & WLAN_STA_ASSOC) && !sta->ap && sta->local)
541                         hostap_event_expired_sta(sta->local->dev, sta);
542                 ap_free_sta(ap, sta);
543         }
544         spin_unlock_bh(&ap->sta_table_lock);
545 }
546
547
548 #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
549
550 static int prism2_ap_proc_show(struct seq_file *m, void *v)
551 {
552         struct sta_info *sta = v;
553         int i;
554
555         if (v == SEQ_START_TOKEN) {
556                 seq_printf(m, "# BSSID CHAN SIGNAL NOISE RATE SSID FLAGS\n");
557                 return 0;
558         }
559
560         if (!sta->ap)
561                 return 0;
562
563         seq_printf(m, "%pM %d %d %d %d '",
564                    sta->addr,
565                    sta->u.ap.channel, sta->last_rx_signal,
566                    sta->last_rx_silence, sta->last_rx_rate);
567
568         for (i = 0; i < sta->u.ap.ssid_len; i++) {
569                 if (sta->u.ap.ssid[i] >= 32 && sta->u.ap.ssid[i] < 127)
570                         seq_putc(m, sta->u.ap.ssid[i]);
571                 else
572                         seq_printf(m, "<%02x>", sta->u.ap.ssid[i]);
573         }
574
575         seq_putc(m, '\'');
576         if (sta->capability & WLAN_CAPABILITY_ESS)
577                 seq_puts(m, " [ESS]");
578         if (sta->capability & WLAN_CAPABILITY_IBSS)
579                 seq_puts(m, " [IBSS]");
580         if (sta->capability & WLAN_CAPABILITY_PRIVACY)
581                 seq_puts(m, " [WEP]");
582         seq_putc(m, '\n');
583         return 0;
584 }
585
586 static void *prism2_ap_proc_start(struct seq_file *m, loff_t *_pos)
587 {
588         struct ap_data *ap = m->private;
589         spin_lock_bh(&ap->sta_table_lock);
590         return seq_list_start_head(&ap->sta_list, *_pos);
591 }
592
593 static void *prism2_ap_proc_next(struct seq_file *m, void *v, loff_t *_pos)
594 {
595         struct ap_data *ap = m->private;
596         return seq_list_next(v, &ap->sta_list, _pos);
597 }
598
599 static void prism2_ap_proc_stop(struct seq_file *m, void *v)
600 {
601         struct ap_data *ap = m->private;
602         spin_unlock_bh(&ap->sta_table_lock);
603 }
604
605 static const struct seq_operations prism2_ap_proc_seqops = {
606         .start  = prism2_ap_proc_start,
607         .next   = prism2_ap_proc_next,
608         .stop   = prism2_ap_proc_stop,
609         .show   = prism2_ap_proc_show,
610 };
611
612 static int prism2_ap_proc_open(struct inode *inode, struct file *file)
613 {
614         int ret = seq_open(file, &prism2_ap_proc_seqops);
615         if (ret == 0) {
616                 struct seq_file *m = file->private_data;
617                 m->private = PDE_DATA(inode);
618         }
619         return ret;
620 }
621
622 static const struct file_operations prism2_ap_proc_fops = {
623         .open           = prism2_ap_proc_open,
624         .read           = seq_read,
625         .llseek         = seq_lseek,
626         .release        = seq_release,
627 };
628 #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
629
630
631 void hostap_check_sta_fw_version(struct ap_data *ap, int sta_fw_ver)
632 {
633         if (!ap)
634                 return;
635
636         if (sta_fw_ver == PRISM2_FW_VER(0,8,0)) {
637                 PDEBUG(DEBUG_AP, "Using data::nullfunc ACK workaround - "
638                        "firmware upgrade recommended\n");
639                 ap->nullfunc_ack = 1;
640         } else
641                 ap->nullfunc_ack = 0;
642
643         if (sta_fw_ver == PRISM2_FW_VER(1,4,2)) {
644                 printk(KERN_WARNING "%s: Warning: secondary station firmware "
645                        "version 1.4.2 does not seem to work in Host AP mode\n",
646                        ap->local->dev->name);
647         }
648 }
649
650
651 /* Called only as a tasklet (software IRQ) */
652 static void hostap_ap_tx_cb(struct sk_buff *skb, int ok, void *data)
653 {
654         struct ap_data *ap = data;
655         struct ieee80211_hdr *hdr;
656
657         if (!ap->local->hostapd || !ap->local->apdev) {
658                 dev_kfree_skb(skb);
659                 return;
660         }
661
662         /* Pass the TX callback frame to the hostapd; use 802.11 header version
663          * 1 to indicate failure (no ACK) and 2 success (frame ACKed) */
664
665         hdr = (struct ieee80211_hdr *) skb->data;
666         hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_VERS);
667         hdr->frame_control |= cpu_to_le16(ok ? BIT(1) : BIT(0));
668
669         skb->dev = ap->local->apdev;
670         skb_pull(skb, hostap_80211_get_hdrlen(hdr->frame_control));
671         skb->pkt_type = PACKET_OTHERHOST;
672         skb->protocol = cpu_to_be16(ETH_P_802_2);
673         memset(skb->cb, 0, sizeof(skb->cb));
674         netif_rx(skb);
675 }
676
677
678 #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
679 /* Called only as a tasklet (software IRQ) */
680 static void hostap_ap_tx_cb_auth(struct sk_buff *skb, int ok, void *data)
681 {
682         struct ap_data *ap = data;
683         struct net_device *dev = ap->local->dev;
684         struct ieee80211_hdr *hdr;
685         u16 auth_alg, auth_transaction, status;
686         __le16 *pos;
687         struct sta_info *sta = NULL;
688         char *txt = NULL;
689
690         if (ap->local->hostapd) {
691                 dev_kfree_skb(skb);
692                 return;
693         }
694
695         hdr = (struct ieee80211_hdr *) skb->data;
696         if (!ieee80211_is_auth(hdr->frame_control) ||
697             skb->len < IEEE80211_MGMT_HDR_LEN + 6) {
698                 printk(KERN_DEBUG "%s: hostap_ap_tx_cb_auth received invalid "
699                        "frame\n", dev->name);
700                 dev_kfree_skb(skb);
701                 return;
702         }
703
704         pos = (__le16 *) (skb->data + IEEE80211_MGMT_HDR_LEN);
705         auth_alg = le16_to_cpu(*pos++);
706         auth_transaction = le16_to_cpu(*pos++);
707         status = le16_to_cpu(*pos++);
708
709         if (!ok) {
710                 txt = "frame was not ACKed";
711                 goto done;
712         }
713
714         spin_lock(&ap->sta_table_lock);
715         sta = ap_get_sta(ap, hdr->addr1);
716         if (sta)
717                 atomic_inc(&sta->users);
718         spin_unlock(&ap->sta_table_lock);
719
720         if (!sta) {
721                 txt = "STA not found";
722                 goto done;
723         }
724
725         if (status == WLAN_STATUS_SUCCESS &&
726             ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
727              (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
728                 txt = "STA authenticated";
729                 sta->flags |= WLAN_STA_AUTH;
730                 sta->last_auth = jiffies;
731         } else if (status != WLAN_STATUS_SUCCESS)
732                 txt = "authentication failed";
733
734  done:
735         if (sta)
736                 atomic_dec(&sta->users);
737         if (txt) {
738                 PDEBUG(DEBUG_AP, "%s: %pM auth_cb - alg=%d "
739                        "trans#=%d status=%d - %s\n",
740                        dev->name, hdr->addr1,
741                        auth_alg, auth_transaction, status, txt);
742         }
743         dev_kfree_skb(skb);
744 }
745
746
747 /* Called only as a tasklet (software IRQ) */
748 static void hostap_ap_tx_cb_assoc(struct sk_buff *skb, int ok, void *data)
749 {
750         struct ap_data *ap = data;
751         struct net_device *dev = ap->local->dev;
752         struct ieee80211_hdr *hdr;
753         u16 status;
754         __le16 *pos;
755         struct sta_info *sta = NULL;
756         char *txt = NULL;
757
758         if (ap->local->hostapd) {
759                 dev_kfree_skb(skb);
760                 return;
761         }
762
763         hdr = (struct ieee80211_hdr *) skb->data;
764         if ((!ieee80211_is_assoc_resp(hdr->frame_control) &&
765              !ieee80211_is_reassoc_resp(hdr->frame_control)) ||
766             skb->len < IEEE80211_MGMT_HDR_LEN + 4) {
767                 printk(KERN_DEBUG "%s: hostap_ap_tx_cb_assoc received invalid "
768                        "frame\n", dev->name);
769                 dev_kfree_skb(skb);
770                 return;
771         }
772
773         if (!ok) {
774                 txt = "frame was not ACKed";
775                 goto done;
776         }
777
778         spin_lock(&ap->sta_table_lock);
779         sta = ap_get_sta(ap, hdr->addr1);
780         if (sta)
781                 atomic_inc(&sta->users);
782         spin_unlock(&ap->sta_table_lock);
783
784         if (!sta) {
785                 txt = "STA not found";
786                 goto done;
787         }
788
789         pos = (__le16 *) (skb->data + IEEE80211_MGMT_HDR_LEN);
790         pos++;
791         status = le16_to_cpu(*pos++);
792         if (status == WLAN_STATUS_SUCCESS) {
793                 if (!(sta->flags & WLAN_STA_ASSOC))
794                         hostap_event_new_sta(dev, sta);
795                 txt = "STA associated";
796                 sta->flags |= WLAN_STA_ASSOC;
797                 sta->last_assoc = jiffies;
798         } else
799                 txt = "association failed";
800
801  done:
802         if (sta)
803                 atomic_dec(&sta->users);
804         if (txt) {
805                 PDEBUG(DEBUG_AP, "%s: %pM assoc_cb - %s\n",
806                        dev->name, hdr->addr1, txt);
807         }
808         dev_kfree_skb(skb);
809 }
810
811 /* Called only as a tasklet (software IRQ); TX callback for poll frames used
812  * in verifying whether the STA is still present. */
813 static void hostap_ap_tx_cb_poll(struct sk_buff *skb, int ok, void *data)
814 {
815         struct ap_data *ap = data;
816         struct ieee80211_hdr *hdr;
817         struct sta_info *sta;
818
819         if (skb->len < 24)
820                 goto fail;
821         hdr = (struct ieee80211_hdr *) skb->data;
822         if (ok) {
823                 spin_lock(&ap->sta_table_lock);
824                 sta = ap_get_sta(ap, hdr->addr1);
825                 if (sta)
826                         sta->flags &= ~WLAN_STA_PENDING_POLL;
827                 spin_unlock(&ap->sta_table_lock);
828         } else {
829                 PDEBUG(DEBUG_AP,
830                        "%s: STA %pM did not ACK activity poll frame\n",
831                        ap->local->dev->name, hdr->addr1);
832         }
833
834  fail:
835         dev_kfree_skb(skb);
836 }
837 #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
838
839
840 void hostap_init_data(local_info_t *local)
841 {
842         struct ap_data *ap = local->ap;
843
844         if (ap == NULL) {
845                 printk(KERN_WARNING "hostap_init_data: ap == NULL\n");
846                 return;
847         }
848         memset(ap, 0, sizeof(struct ap_data));
849         ap->local = local;
850
851         ap->ap_policy = GET_INT_PARM(other_ap_policy, local->card_idx);
852         ap->bridge_packets = GET_INT_PARM(ap_bridge_packets, local->card_idx);
853         ap->max_inactivity =
854                 GET_INT_PARM(ap_max_inactivity, local->card_idx) * HZ;
855         ap->autom_ap_wds = GET_INT_PARM(autom_ap_wds, local->card_idx);
856
857         spin_lock_init(&ap->sta_table_lock);
858         INIT_LIST_HEAD(&ap->sta_list);
859
860         /* Initialize task queue structure for AP management */
861         INIT_WORK(&local->ap->add_sta_proc_queue, handle_add_proc_queue);
862
863         ap->tx_callback_idx =
864                 hostap_tx_callback_register(local, hostap_ap_tx_cb, ap);
865         if (ap->tx_callback_idx == 0)
866                 printk(KERN_WARNING "%s: failed to register TX callback for "
867                        "AP\n", local->dev->name);
868 #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
869         INIT_WORK(&local->ap->wds_oper_queue, handle_wds_oper_queue);
870
871         ap->tx_callback_auth =
872                 hostap_tx_callback_register(local, hostap_ap_tx_cb_auth, ap);
873         ap->tx_callback_assoc =
874                 hostap_tx_callback_register(local, hostap_ap_tx_cb_assoc, ap);
875         ap->tx_callback_poll =
876                 hostap_tx_callback_register(local, hostap_ap_tx_cb_poll, ap);
877         if (ap->tx_callback_auth == 0 || ap->tx_callback_assoc == 0 ||
878                 ap->tx_callback_poll == 0)
879                 printk(KERN_WARNING "%s: failed to register TX callback for "
880                        "AP\n", local->dev->name);
881
882         spin_lock_init(&ap->mac_restrictions.lock);
883         INIT_LIST_HEAD(&ap->mac_restrictions.mac_list);
884 #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
885
886         ap->initialized = 1;
887 }
888
889
890 void hostap_init_ap_proc(local_info_t *local)
891 {
892         struct ap_data *ap = local->ap;
893
894         ap->proc = local->proc;
895         if (ap->proc == NULL)
896                 return;
897
898 #ifndef PRISM2_NO_PROCFS_DEBUG
899         proc_create_data("ap_debug", 0, ap->proc, &ap_debug_proc_fops, ap);
900 #endif /* PRISM2_NO_PROCFS_DEBUG */
901
902 #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
903         proc_create_data("ap_control", 0, ap->proc, &ap_control_proc_fops, ap);
904         proc_create_data("ap", 0, ap->proc, &prism2_ap_proc_fops, ap);
905 #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
906
907 }
908
909
910 void hostap_free_data(struct ap_data *ap)
911 {
912         struct sta_info *n, *sta;
913
914         if (ap == NULL || !ap->initialized) {
915                 printk(KERN_DEBUG "hostap_free_data: ap has not yet been "
916                        "initialized - skip resource freeing\n");
917                 return;
918         }
919
920         flush_work(&ap->add_sta_proc_queue);
921
922 #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
923         flush_work(&ap->wds_oper_queue);
924         if (ap->crypt)
925                 ap->crypt->deinit(ap->crypt_priv);
926         ap->crypt = ap->crypt_priv = NULL;
927 #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
928
929         list_for_each_entry_safe(sta, n, &ap->sta_list, list) {
930                 ap_sta_hash_del(ap, sta);
931                 list_del(&sta->list);
932                 if ((sta->flags & WLAN_STA_ASSOC) && !sta->ap && sta->local)
933                         hostap_event_expired_sta(sta->local->dev, sta);
934                 ap_free_sta(ap, sta);
935         }
936
937 #ifndef PRISM2_NO_PROCFS_DEBUG
938         if (ap->proc != NULL) {
939                 remove_proc_entry("ap_debug", ap->proc);
940         }
941 #endif /* PRISM2_NO_PROCFS_DEBUG */
942
943 #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
944         if (ap->proc != NULL) {
945           remove_proc_entry("ap", ap->proc);
946                 remove_proc_entry("ap_control", ap->proc);
947         }
948         ap_control_flush_macs(&ap->mac_restrictions);
949 #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
950
951         ap->initialized = 0;
952 }
953
954
955 /* caller should have mutex for AP STA list handling */
956 static struct sta_info* ap_get_sta(struct ap_data *ap, u8 *sta)
957 {
958         struct sta_info *s;
959
960         s = ap->sta_hash[STA_HASH(sta)];
961         while (s != NULL && !ether_addr_equal(s->addr, sta))
962                 s = s->hnext;
963         return s;
964 }
965
966
967 #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
968
969 /* Called from timer handler and from scheduled AP queue handlers */
970 static void prism2_send_mgmt(struct net_device *dev,
971                              u16 type_subtype, char *body,
972                              int body_len, u8 *addr, u16 tx_cb_idx)
973 {
974         struct hostap_interface *iface;
975         local_info_t *local;
976         struct ieee80211_hdr *hdr;
977         u16 fc;
978         struct sk_buff *skb;
979         struct hostap_skb_tx_data *meta;
980         int hdrlen;
981
982         iface = netdev_priv(dev);
983         local = iface->local;
984         dev = local->dev; /* always use master radio device */
985         iface = netdev_priv(dev);
986
987         if (!(dev->flags & IFF_UP)) {
988                 PDEBUG(DEBUG_AP, "%s: prism2_send_mgmt - device is not UP - "
989                        "cannot send frame\n", dev->name);
990                 return;
991         }
992
993         skb = dev_alloc_skb(sizeof(*hdr) + body_len);
994         if (skb == NULL) {
995                 PDEBUG(DEBUG_AP, "%s: prism2_send_mgmt failed to allocate "
996                        "skb\n", dev->name);
997                 return;
998         }
999
1000         fc = type_subtype;
1001         hdrlen = hostap_80211_get_hdrlen(cpu_to_le16(type_subtype));
1002         hdr = skb_put_zero(skb, hdrlen);
1003         if (body)
1004                 skb_put_data(skb, body, body_len);
1005
1006         /* FIX: ctrl::ack sending used special HFA384X_TX_CTRL_802_11
1007          * tx_control instead of using local->tx_control */
1008
1009
1010         memcpy(hdr->addr1, addr, ETH_ALEN); /* DA / RA */
1011         if (ieee80211_is_data(hdr->frame_control)) {
1012                 fc |= IEEE80211_FCTL_FROMDS;
1013                 memcpy(hdr->addr2, dev->dev_addr, ETH_ALEN); /* BSSID */
1014                 memcpy(hdr->addr3, dev->dev_addr, ETH_ALEN); /* SA */
1015         } else if (ieee80211_is_ctl(hdr->frame_control)) {
1016                 /* control:ACK does not have addr2 or addr3 */
1017                 eth_zero_addr(hdr->addr2);
1018                 eth_zero_addr(hdr->addr3);
1019         } else {
1020                 memcpy(hdr->addr2, dev->dev_addr, ETH_ALEN); /* SA */
1021                 memcpy(hdr->addr3, dev->dev_addr, ETH_ALEN); /* BSSID */
1022         }
1023
1024         hdr->frame_control = cpu_to_le16(fc);
1025
1026         meta = (struct hostap_skb_tx_data *) skb->cb;
1027         memset(meta, 0, sizeof(*meta));
1028         meta->magic = HOSTAP_SKB_TX_DATA_MAGIC;
1029         meta->iface = iface;
1030         meta->tx_cb_idx = tx_cb_idx;
1031
1032         skb->dev = dev;
1033         skb_reset_mac_header(skb);
1034         skb_reset_network_header(skb);
1035         dev_queue_xmit(skb);
1036 }
1037 #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
1038
1039
1040 static int prism2_sta_proc_show(struct seq_file *m, void *v)
1041 {
1042         struct sta_info *sta = m->private;
1043         int i;
1044
1045         /* FIX: possible race condition.. the STA data could have just expired,
1046          * but proc entry was still here so that the read could have started;
1047          * some locking should be done here.. */
1048
1049         seq_printf(m,
1050                    "%s=%pM\nusers=%d\naid=%d\n"
1051                    "flags=0x%04x%s%s%s%s%s%s%s\n"
1052                    "capability=0x%02x\nlisten_interval=%d\nsupported_rates=",
1053                    sta->ap ? "AP" : "STA",
1054                    sta->addr, atomic_read(&sta->users), sta->aid,
1055                    sta->flags,
1056                    sta->flags & WLAN_STA_AUTH ? " AUTH" : "",
1057                    sta->flags & WLAN_STA_ASSOC ? " ASSOC" : "",
1058                    sta->flags & WLAN_STA_PS ? " PS" : "",
1059                    sta->flags & WLAN_STA_TIM ? " TIM" : "",
1060                    sta->flags & WLAN_STA_PERM ? " PERM" : "",
1061                    sta->flags & WLAN_STA_AUTHORIZED ? " AUTHORIZED" : "",
1062                    sta->flags & WLAN_STA_PENDING_POLL ? " POLL" : "",
1063                    sta->capability, sta->listen_interval);
1064         /* supported_rates: 500 kbit/s units with msb ignored */
1065         for (i = 0; i < sizeof(sta->supported_rates); i++)
1066                 if (sta->supported_rates[i] != 0)
1067                         seq_printf(m, "%d%sMbps ",
1068                                    (sta->supported_rates[i] & 0x7f) / 2,
1069                                    sta->supported_rates[i] & 1 ? ".5" : "");
1070         seq_printf(m,
1071                    "\njiffies=%lu\nlast_auth=%lu\nlast_assoc=%lu\n"
1072                    "last_rx=%lu\nlast_tx=%lu\nrx_packets=%lu\n"
1073                    "tx_packets=%lu\n"
1074                    "rx_bytes=%lu\ntx_bytes=%lu\nbuffer_count=%d\n"
1075                    "last_rx: silence=%d dBm signal=%d dBm rate=%d%s Mbps\n"
1076                    "tx_rate=%d\ntx[1M]=%d\ntx[2M]=%d\ntx[5.5M]=%d\n"
1077                    "tx[11M]=%d\n"
1078                    "rx[1M]=%d\nrx[2M]=%d\nrx[5.5M]=%d\nrx[11M]=%d\n",
1079                    jiffies, sta->last_auth, sta->last_assoc, sta->last_rx,
1080                    sta->last_tx,
1081                    sta->rx_packets, sta->tx_packets, sta->rx_bytes,
1082                    sta->tx_bytes, skb_queue_len(&sta->tx_buf),
1083                    sta->last_rx_silence,
1084                    sta->last_rx_signal, sta->last_rx_rate / 10,
1085                    sta->last_rx_rate % 10 ? ".5" : "",
1086                    sta->tx_rate, sta->tx_count[0], sta->tx_count[1],
1087                    sta->tx_count[2], sta->tx_count[3],  sta->rx_count[0],
1088                    sta->rx_count[1], sta->rx_count[2], sta->rx_count[3]);
1089         if (sta->crypt && sta->crypt->ops && sta->crypt->ops->print_stats)
1090                 sta->crypt->ops->print_stats(m, sta->crypt->priv);
1091 #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
1092         if (sta->ap) {
1093                 if (sta->u.ap.channel >= 0)
1094                         seq_printf(m, "channel=%d\n", sta->u.ap.channel);
1095                 seq_puts(m, "ssid=");
1096                 for (i = 0; i < sta->u.ap.ssid_len; i++) {
1097                         if (sta->u.ap.ssid[i] >= 32 && sta->u.ap.ssid[i] < 127)
1098                                 seq_putc(m, sta->u.ap.ssid[i]);
1099                         else
1100                                 seq_printf(m, "<%02x>", sta->u.ap.ssid[i]);
1101                 }
1102                 seq_putc(m, '\n');
1103         }
1104 #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
1105
1106         return 0;
1107 }
1108
1109 static void handle_add_proc_queue(struct work_struct *work)
1110 {
1111         struct ap_data *ap = container_of(work, struct ap_data,
1112                                           add_sta_proc_queue);
1113         struct sta_info *sta;
1114         char name[20];
1115         struct add_sta_proc_data *entry, *prev;
1116
1117         entry = ap->add_sta_proc_entries;
1118         ap->add_sta_proc_entries = NULL;
1119
1120         while (entry) {
1121                 spin_lock_bh(&ap->sta_table_lock);
1122                 sta = ap_get_sta(ap, entry->addr);
1123                 if (sta)
1124                         atomic_inc(&sta->users);
1125                 spin_unlock_bh(&ap->sta_table_lock);
1126
1127                 if (sta) {
1128                         sprintf(name, "%pM", sta->addr);
1129                         sta->proc = proc_create_single_data(
1130                                 name, 0, ap->proc,
1131                                 prism2_sta_proc_show, sta);
1132
1133                         atomic_dec(&sta->users);
1134                 }
1135
1136                 prev = entry;
1137                 entry = entry->next;
1138                 kfree(prev);
1139         }
1140 }
1141
1142
1143 static struct sta_info * ap_add_sta(struct ap_data *ap, u8 *addr)
1144 {
1145         struct sta_info *sta;
1146
1147         sta = kzalloc(sizeof(struct sta_info), GFP_ATOMIC);
1148         if (sta == NULL) {
1149                 PDEBUG(DEBUG_AP, "AP: kmalloc failed\n");
1150                 return NULL;
1151         }
1152
1153         /* initialize STA info data */
1154         sta->local = ap->local;
1155         skb_queue_head_init(&sta->tx_buf);
1156         memcpy(sta->addr, addr, ETH_ALEN);
1157
1158         atomic_inc(&sta->users);
1159         spin_lock_bh(&ap->sta_table_lock);
1160         list_add(&sta->list, &ap->sta_list);
1161         ap->num_sta++;
1162         ap_sta_hash_add(ap, sta);
1163         spin_unlock_bh(&ap->sta_table_lock);
1164
1165         if (ap->proc) {
1166                 struct add_sta_proc_data *entry;
1167                 /* schedule a non-interrupt context process to add a procfs
1168                  * entry for the STA since procfs code use GFP_KERNEL */
1169                 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
1170                 if (entry) {
1171                         memcpy(entry->addr, sta->addr, ETH_ALEN);
1172                         entry->next = ap->add_sta_proc_entries;
1173                         ap->add_sta_proc_entries = entry;
1174                         schedule_work(&ap->add_sta_proc_queue);
1175                 } else
1176                         printk(KERN_DEBUG "Failed to add STA proc data\n");
1177         }
1178
1179 #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
1180         timer_setup(&sta->timer, ap_handle_timer, 0);
1181         sta->timer.expires = jiffies + ap->max_inactivity;
1182         if (!ap->local->hostapd)
1183                 add_timer(&sta->timer);
1184 #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
1185
1186         return sta;
1187 }
1188
1189
1190 static int ap_tx_rate_ok(int rateidx, struct sta_info *sta,
1191                          local_info_t *local)
1192 {
1193         if (rateidx > sta->tx_max_rate ||
1194             !(sta->tx_supp_rates & (1 << rateidx)))
1195                 return 0;
1196
1197         if (local->tx_rate_control != 0 &&
1198             !(local->tx_rate_control & (1 << rateidx)))
1199                 return 0;
1200
1201         return 1;
1202 }
1203
1204
1205 static void prism2_check_tx_rates(struct sta_info *sta)
1206 {
1207         int i;
1208
1209         sta->tx_supp_rates = 0;
1210         for (i = 0; i < sizeof(sta->supported_rates); i++) {
1211                 if ((sta->supported_rates[i] & 0x7f) == 2)
1212                         sta->tx_supp_rates |= WLAN_RATE_1M;
1213                 if ((sta->supported_rates[i] & 0x7f) == 4)
1214                         sta->tx_supp_rates |= WLAN_RATE_2M;
1215                 if ((sta->supported_rates[i] & 0x7f) == 11)
1216                         sta->tx_supp_rates |= WLAN_RATE_5M5;
1217                 if ((sta->supported_rates[i] & 0x7f) == 22)
1218                         sta->tx_supp_rates |= WLAN_RATE_11M;
1219         }
1220         sta->tx_max_rate = sta->tx_rate = sta->tx_rate_idx = 0;
1221         if (sta->tx_supp_rates & WLAN_RATE_1M) {
1222                 sta->tx_max_rate = 0;
1223                 if (ap_tx_rate_ok(0, sta, sta->local)) {
1224                         sta->tx_rate = 10;
1225                         sta->tx_rate_idx = 0;
1226                 }
1227         }
1228         if (sta->tx_supp_rates & WLAN_RATE_2M) {
1229                 sta->tx_max_rate = 1;
1230                 if (ap_tx_rate_ok(1, sta, sta->local)) {
1231                         sta->tx_rate = 20;
1232                         sta->tx_rate_idx = 1;
1233                 }
1234         }
1235         if (sta->tx_supp_rates & WLAN_RATE_5M5) {
1236                 sta->tx_max_rate = 2;
1237                 if (ap_tx_rate_ok(2, sta, sta->local)) {
1238                         sta->tx_rate = 55;
1239                         sta->tx_rate_idx = 2;
1240                 }
1241         }
1242         if (sta->tx_supp_rates & WLAN_RATE_11M) {
1243                 sta->tx_max_rate = 3;
1244                 if (ap_tx_rate_ok(3, sta, sta->local)) {
1245                         sta->tx_rate = 110;
1246                         sta->tx_rate_idx = 3;
1247                 }
1248         }
1249 }
1250
1251
1252 #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
1253
1254 static void ap_crypt_init(struct ap_data *ap)
1255 {
1256         ap->crypt = lib80211_get_crypto_ops("WEP");
1257
1258         if (ap->crypt) {
1259                 if (ap->crypt->init) {
1260                         ap->crypt_priv = ap->crypt->init(0);
1261                         if (ap->crypt_priv == NULL)
1262                                 ap->crypt = NULL;
1263                         else {
1264                                 u8 key[WEP_KEY_LEN];
1265                                 get_random_bytes(key, WEP_KEY_LEN);
1266                                 ap->crypt->set_key(key, WEP_KEY_LEN, NULL,
1267                                                    ap->crypt_priv);
1268                         }
1269                 }
1270         }
1271
1272         if (ap->crypt == NULL) {
1273                 printk(KERN_WARNING "AP could not initialize WEP: load module "
1274                        "lib80211_crypt_wep.ko\n");
1275         }
1276 }
1277
1278
1279 /* Generate challenge data for shared key authentication. IEEE 802.11 specifies
1280  * that WEP algorithm is used for generating challenge. This should be unique,
1281  * but otherwise there is not really need for randomness etc. Initialize WEP
1282  * with pseudo random key and then use increasing IV to get unique challenge
1283  * streams.
1284  *
1285  * Called only as a scheduled task for pending AP frames.
1286  */
1287 static char * ap_auth_make_challenge(struct ap_data *ap)
1288 {
1289         char *tmpbuf;
1290         struct sk_buff *skb;
1291
1292         if (ap->crypt == NULL) {
1293                 ap_crypt_init(ap);
1294                 if (ap->crypt == NULL)
1295                         return NULL;
1296         }
1297
1298         tmpbuf = kmalloc(WLAN_AUTH_CHALLENGE_LEN, GFP_ATOMIC);
1299         if (tmpbuf == NULL) {
1300                 PDEBUG(DEBUG_AP, "AP: kmalloc failed for challenge\n");
1301                 return NULL;
1302         }
1303
1304         skb = dev_alloc_skb(WLAN_AUTH_CHALLENGE_LEN +
1305                             ap->crypt->extra_mpdu_prefix_len +
1306                             ap->crypt->extra_mpdu_postfix_len);
1307         if (skb == NULL) {
1308                 kfree(tmpbuf);
1309                 return NULL;
1310         }
1311
1312         skb_reserve(skb, ap->crypt->extra_mpdu_prefix_len);
1313         skb_put_zero(skb, WLAN_AUTH_CHALLENGE_LEN);
1314         if (ap->crypt->encrypt_mpdu(skb, 0, ap->crypt_priv)) {
1315                 dev_kfree_skb(skb);
1316                 kfree(tmpbuf);
1317                 return NULL;
1318         }
1319
1320         skb_copy_from_linear_data_offset(skb, ap->crypt->extra_mpdu_prefix_len,
1321                                          tmpbuf, WLAN_AUTH_CHALLENGE_LEN);
1322         dev_kfree_skb(skb);
1323
1324         return tmpbuf;
1325 }
1326
1327
1328 /* Called only as a scheduled task for pending AP frames. */
1329 static void handle_authen(local_info_t *local, struct sk_buff *skb,
1330                           struct hostap_80211_rx_status *rx_stats)
1331 {
1332         struct net_device *dev = local->dev;
1333         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1334         size_t hdrlen;
1335         struct ap_data *ap = local->ap;
1336         char body[8 + WLAN_AUTH_CHALLENGE_LEN], *challenge = NULL;
1337         int len, olen;
1338         u16 auth_alg, auth_transaction, status_code;
1339         __le16 *pos;
1340         u16 resp = WLAN_STATUS_SUCCESS;
1341         struct sta_info *sta = NULL;
1342         struct lib80211_crypt_data *crypt;
1343         char *txt = "";
1344
1345         len = skb->len - IEEE80211_MGMT_HDR_LEN;
1346
1347         hdrlen = hostap_80211_get_hdrlen(hdr->frame_control);
1348
1349         if (len < 6) {
1350                 PDEBUG(DEBUG_AP, "%s: handle_authen - too short payload "
1351                        "(len=%d) from %pM\n", dev->name, len, hdr->addr2);
1352                 return;
1353         }
1354
1355         spin_lock_bh(&local->ap->sta_table_lock);
1356         sta = ap_get_sta(local->ap, hdr->addr2);
1357         if (sta)
1358                 atomic_inc(&sta->users);
1359         spin_unlock_bh(&local->ap->sta_table_lock);
1360
1361         if (sta && sta->crypt)
1362                 crypt = sta->crypt;
1363         else {
1364                 int idx = 0;
1365                 if (skb->len >= hdrlen + 3)
1366                         idx = skb->data[hdrlen + 3] >> 6;
1367                 crypt = local->crypt_info.crypt[idx];
1368         }
1369
1370         pos = (__le16 *) (skb->data + IEEE80211_MGMT_HDR_LEN);
1371         auth_alg = __le16_to_cpu(*pos);
1372         pos++;
1373         auth_transaction = __le16_to_cpu(*pos);
1374         pos++;
1375         status_code = __le16_to_cpu(*pos);
1376         pos++;
1377
1378         if (ether_addr_equal(dev->dev_addr, hdr->addr2) ||
1379             ap_control_mac_deny(&ap->mac_restrictions, hdr->addr2)) {
1380                 txt = "authentication denied";
1381                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1382                 goto fail;
1383         }
1384
1385         if (((local->auth_algs & PRISM2_AUTH_OPEN) &&
1386              auth_alg == WLAN_AUTH_OPEN) ||
1387             ((local->auth_algs & PRISM2_AUTH_SHARED_KEY) &&
1388              crypt && auth_alg == WLAN_AUTH_SHARED_KEY)) {
1389         } else {
1390                 txt = "unsupported algorithm";
1391                 resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
1392                 goto fail;
1393         }
1394
1395         if (len >= 8) {
1396                 u8 *u = (u8 *) pos;
1397                 if (*u == WLAN_EID_CHALLENGE) {
1398                         if (*(u + 1) != WLAN_AUTH_CHALLENGE_LEN) {
1399                                 txt = "invalid challenge len";
1400                                 resp = WLAN_STATUS_CHALLENGE_FAIL;
1401                                 goto fail;
1402                         }
1403                         if (len - 8 < WLAN_AUTH_CHALLENGE_LEN) {
1404                                 txt = "challenge underflow";
1405                                 resp = WLAN_STATUS_CHALLENGE_FAIL;
1406                                 goto fail;
1407                         }
1408                         challenge = (char *) (u + 2);
1409                 }
1410         }
1411
1412         if (sta && sta->ap) {
1413                 if (time_after(jiffies, sta->u.ap.last_beacon +
1414                                (10 * sta->listen_interval * HZ) / 1024)) {
1415                         PDEBUG(DEBUG_AP, "%s: no beacons received for a while,"
1416                                " assuming AP %pM is now STA\n",
1417                                dev->name, sta->addr);
1418                         sta->ap = 0;
1419                         sta->flags = 0;
1420                         sta->u.sta.challenge = NULL;
1421                 } else {
1422                         txt = "AP trying to authenticate?";
1423                         resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1424                         goto fail;
1425                 }
1426         }
1427
1428         if ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 1) ||
1429             (auth_alg == WLAN_AUTH_SHARED_KEY &&
1430              (auth_transaction == 1 ||
1431               (auth_transaction == 3 && sta != NULL &&
1432                sta->u.sta.challenge != NULL)))) {
1433         } else {
1434                 txt = "unknown authentication transaction number";
1435                 resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
1436                 goto fail;
1437         }
1438
1439         if (sta == NULL) {
1440                 txt = "new STA";
1441
1442                 if (local->ap->num_sta >= MAX_STA_COUNT) {
1443                         /* FIX: might try to remove some old STAs first? */
1444                         txt = "no more room for new STAs";
1445                         resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1446                         goto fail;
1447                 }
1448
1449                 sta = ap_add_sta(local->ap, hdr->addr2);
1450                 if (sta == NULL) {
1451                         txt = "ap_add_sta failed";
1452                         resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1453                         goto fail;
1454                 }
1455         }
1456
1457         switch (auth_alg) {
1458         case WLAN_AUTH_OPEN:
1459                 txt = "authOK";
1460                 /* IEEE 802.11 standard is not completely clear about
1461                  * whether STA is considered authenticated after
1462                  * authentication OK frame has been send or after it
1463                  * has been ACKed. In order to reduce interoperability
1464                  * issues, mark the STA authenticated before ACK. */
1465                 sta->flags |= WLAN_STA_AUTH;
1466                 break;
1467
1468         case WLAN_AUTH_SHARED_KEY:
1469                 if (auth_transaction == 1) {
1470                         if (sta->u.sta.challenge == NULL) {
1471                                 sta->u.sta.challenge =
1472                                         ap_auth_make_challenge(local->ap);
1473                                 if (sta->u.sta.challenge == NULL) {
1474                                         resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1475                                         goto fail;
1476                                 }
1477                         }
1478                 } else {
1479                         if (sta->u.sta.challenge == NULL ||
1480                             challenge == NULL ||
1481                             memcmp(sta->u.sta.challenge, challenge,
1482                                    WLAN_AUTH_CHALLENGE_LEN) != 0 ||
1483                             !ieee80211_has_protected(hdr->frame_control)) {
1484                                 txt = "challenge response incorrect";
1485                                 resp = WLAN_STATUS_CHALLENGE_FAIL;
1486                                 goto fail;
1487                         }
1488
1489                         txt = "challenge OK - authOK";
1490                         /* IEEE 802.11 standard is not completely clear about
1491                          * whether STA is considered authenticated after
1492                          * authentication OK frame has been send or after it
1493                          * has been ACKed. In order to reduce interoperability
1494                          * issues, mark the STA authenticated before ACK. */
1495                         sta->flags |= WLAN_STA_AUTH;
1496                         kfree(sta->u.sta.challenge);
1497                         sta->u.sta.challenge = NULL;
1498                 }
1499                 break;
1500         }
1501
1502  fail:
1503         pos = (__le16 *) body;
1504         *pos = cpu_to_le16(auth_alg);
1505         pos++;
1506         *pos = cpu_to_le16(auth_transaction + 1);
1507         pos++;
1508         *pos = cpu_to_le16(resp); /* status_code */
1509         pos++;
1510         olen = 6;
1511
1512         if (resp == WLAN_STATUS_SUCCESS && sta != NULL &&
1513             sta->u.sta.challenge != NULL &&
1514             auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 1) {
1515                 u8 *tmp = (u8 *) pos;
1516                 *tmp++ = WLAN_EID_CHALLENGE;
1517                 *tmp++ = WLAN_AUTH_CHALLENGE_LEN;
1518                 pos++;
1519                 memcpy(pos, sta->u.sta.challenge, WLAN_AUTH_CHALLENGE_LEN);
1520                 olen += 2 + WLAN_AUTH_CHALLENGE_LEN;
1521         }
1522
1523         prism2_send_mgmt(dev, IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_AUTH,
1524                          body, olen, hdr->addr2, ap->tx_callback_auth);
1525
1526         if (sta) {
1527                 sta->last_rx = jiffies;
1528                 atomic_dec(&sta->users);
1529         }
1530
1531         if (resp) {
1532                 PDEBUG(DEBUG_AP, "%s: %pM auth (alg=%d "
1533                        "trans#=%d stat=%d len=%d fc=%04x) ==> %d (%s)\n",
1534                        dev->name, hdr->addr2,
1535                        auth_alg, auth_transaction, status_code, len,
1536                        le16_to_cpu(hdr->frame_control), resp, txt);
1537         }
1538 }
1539
1540
1541 /* Called only as a scheduled task for pending AP frames. */
1542 static void handle_assoc(local_info_t *local, struct sk_buff *skb,
1543                          struct hostap_80211_rx_status *rx_stats, int reassoc)
1544 {
1545         struct net_device *dev = local->dev;
1546         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1547         char body[12], *p, *lpos;
1548         int len, left;
1549         __le16 *pos;
1550         u16 resp = WLAN_STATUS_SUCCESS;
1551         struct sta_info *sta = NULL;
1552         int send_deauth = 0;
1553         char *txt = "";
1554         u8 prev_ap[ETH_ALEN];
1555
1556         left = len = skb->len - IEEE80211_MGMT_HDR_LEN;
1557
1558         if (len < (reassoc ? 10 : 4)) {
1559                 PDEBUG(DEBUG_AP, "%s: handle_assoc - too short payload "
1560                        "(len=%d, reassoc=%d) from %pM\n",
1561                        dev->name, len, reassoc, hdr->addr2);
1562                 return;
1563         }
1564
1565         spin_lock_bh(&local->ap->sta_table_lock);
1566         sta = ap_get_sta(local->ap, hdr->addr2);
1567         if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
1568                 spin_unlock_bh(&local->ap->sta_table_lock);
1569                 txt = "trying to associate before authentication";
1570                 send_deauth = 1;
1571                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1572                 sta = NULL; /* do not decrement sta->users */
1573                 goto fail;
1574         }
1575         atomic_inc(&sta->users);
1576         spin_unlock_bh(&local->ap->sta_table_lock);
1577
1578         pos = (__le16 *) (skb->data + IEEE80211_MGMT_HDR_LEN);
1579         sta->capability = __le16_to_cpu(*pos);
1580         pos++; left -= 2;
1581         sta->listen_interval = __le16_to_cpu(*pos);
1582         pos++; left -= 2;
1583
1584         if (reassoc) {
1585                 memcpy(prev_ap, pos, ETH_ALEN);
1586                 pos++; pos++; pos++; left -= 6;
1587         } else
1588                 eth_zero_addr(prev_ap);
1589
1590         if (left >= 2) {
1591                 unsigned int ileft;
1592                 unsigned char *u = (unsigned char *) pos;
1593
1594                 if (*u == WLAN_EID_SSID) {
1595                         u++; left--;
1596                         ileft = *u;
1597                         u++; left--;
1598
1599                         if (ileft > left || ileft > MAX_SSID_LEN) {
1600                                 txt = "SSID overflow";
1601                                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1602                                 goto fail;
1603                         }
1604
1605                         if (ileft != strlen(local->essid) ||
1606                             memcmp(local->essid, u, ileft) != 0) {
1607                                 txt = "not our SSID";
1608                                 resp = WLAN_STATUS_ASSOC_DENIED_UNSPEC;
1609                                 goto fail;
1610                         }
1611
1612                         u += ileft;
1613                         left -= ileft;
1614                 }
1615
1616                 if (left >= 2 && *u == WLAN_EID_SUPP_RATES) {
1617                         u++; left--;
1618                         ileft = *u;
1619                         u++; left--;
1620
1621                         if (ileft > left || ileft == 0 ||
1622                             ileft > WLAN_SUPP_RATES_MAX) {
1623                                 txt = "SUPP_RATES len error";
1624                                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1625                                 goto fail;
1626                         }
1627
1628                         memset(sta->supported_rates, 0,
1629                                sizeof(sta->supported_rates));
1630                         memcpy(sta->supported_rates, u, ileft);
1631                         prism2_check_tx_rates(sta);
1632
1633                         u += ileft;
1634                         left -= ileft;
1635                 }
1636
1637                 if (left > 0) {
1638                         PDEBUG(DEBUG_AP, "%s: assoc from %pM"
1639                                " with extra data (%d bytes) [",
1640                                dev->name, hdr->addr2, left);
1641                         while (left > 0) {
1642                                 PDEBUG2(DEBUG_AP, "<%02x>", *u);
1643                                 u++; left--;
1644                         }
1645                         PDEBUG2(DEBUG_AP, "]\n");
1646                 }
1647         } else {
1648                 txt = "frame underflow";
1649                 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1650                 goto fail;
1651         }
1652
1653         /* get a unique AID */
1654         if (sta->aid > 0)
1655                 txt = "OK, old AID";
1656         else {
1657                 spin_lock_bh(&local->ap->sta_table_lock);
1658                 for (sta->aid = 1; sta->aid <= MAX_AID_TABLE_SIZE; sta->aid++)
1659                         if (local->ap->sta_aid[sta->aid - 1] == NULL)
1660                                 break;
1661                 if (sta->aid > MAX_AID_TABLE_SIZE) {
1662                         sta->aid = 0;
1663                         spin_unlock_bh(&local->ap->sta_table_lock);
1664                         resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
1665                         txt = "no room for more AIDs";
1666                 } else {
1667                         local->ap->sta_aid[sta->aid - 1] = sta;
1668                         spin_unlock_bh(&local->ap->sta_table_lock);
1669                         txt = "OK, new AID";
1670                 }
1671         }
1672
1673  fail:
1674         pos = (__le16 *) body;
1675
1676         if (send_deauth) {
1677                 *pos = cpu_to_le16(WLAN_REASON_STA_REQ_ASSOC_WITHOUT_AUTH);
1678                 pos++;
1679         } else {
1680                 /* FIX: CF-Pollable and CF-PollReq should be set to match the
1681                  * values in beacons/probe responses */
1682                 /* FIX: how about privacy and WEP? */
1683                 /* capability */
1684                 *pos = cpu_to_le16(WLAN_CAPABILITY_ESS);
1685                 pos++;
1686
1687                 /* status_code */
1688                 *pos = cpu_to_le16(resp);
1689                 pos++;
1690
1691                 *pos = cpu_to_le16((sta && sta->aid > 0 ? sta->aid : 0) |
1692                                      BIT(14) | BIT(15)); /* AID */
1693                 pos++;
1694
1695                 /* Supported rates (Information element) */
1696                 p = (char *) pos;
1697                 *p++ = WLAN_EID_SUPP_RATES;
1698                 lpos = p;
1699                 *p++ = 0; /* len */
1700                 if (local->tx_rate_control & WLAN_RATE_1M) {
1701                         *p++ = local->basic_rates & WLAN_RATE_1M ? 0x82 : 0x02;
1702                         (*lpos)++;
1703                 }
1704                 if (local->tx_rate_control & WLAN_RATE_2M) {
1705                         *p++ = local->basic_rates & WLAN_RATE_2M ? 0x84 : 0x04;
1706                         (*lpos)++;
1707                 }
1708                 if (local->tx_rate_control & WLAN_RATE_5M5) {
1709                         *p++ = local->basic_rates & WLAN_RATE_5M5 ?
1710                                 0x8b : 0x0b;
1711                         (*lpos)++;
1712                 }
1713                 if (local->tx_rate_control & WLAN_RATE_11M) {
1714                         *p++ = local->basic_rates & WLAN_RATE_11M ?
1715                                 0x96 : 0x16;
1716                         (*lpos)++;
1717                 }
1718                 pos = (__le16 *) p;
1719         }
1720
1721         prism2_send_mgmt(dev, IEEE80211_FTYPE_MGMT |
1722                          (send_deauth ? IEEE80211_STYPE_DEAUTH :
1723                           (reassoc ? IEEE80211_STYPE_REASSOC_RESP :
1724                            IEEE80211_STYPE_ASSOC_RESP)),
1725                          body, (u8 *) pos - (u8 *) body,
1726                          hdr->addr2,
1727                          send_deauth ? 0 : local->ap->tx_callback_assoc);
1728
1729         if (sta) {
1730                 if (resp == WLAN_STATUS_SUCCESS) {
1731                         sta->last_rx = jiffies;
1732                         /* STA will be marked associated from TX callback, if
1733                          * AssocResp is ACKed */
1734                 }
1735                 atomic_dec(&sta->users);
1736         }
1737
1738 #if 0
1739         PDEBUG(DEBUG_AP, "%s: %pM %sassoc (len=%d "
1740                "prev_ap=%pM) => %d(%d) (%s)\n",
1741                dev->name,
1742                hdr->addr2,
1743                reassoc ? "re" : "", len,
1744                prev_ap,
1745                resp, send_deauth, txt);
1746 #endif
1747 }
1748
1749
1750 /* Called only as a scheduled task for pending AP frames. */
1751 static void handle_deauth(local_info_t *local, struct sk_buff *skb,
1752                           struct hostap_80211_rx_status *rx_stats)
1753 {
1754         struct net_device *dev = local->dev;
1755         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1756         char *body = (char *) (skb->data + IEEE80211_MGMT_HDR_LEN);
1757         int len;
1758         u16 reason_code;
1759         __le16 *pos;
1760         struct sta_info *sta = NULL;
1761
1762         len = skb->len - IEEE80211_MGMT_HDR_LEN;
1763
1764         if (len < 2) {
1765                 printk("handle_deauth - too short payload (len=%d)\n", len);
1766                 return;
1767         }
1768
1769         pos = (__le16 *) body;
1770         reason_code = le16_to_cpu(*pos);
1771
1772         PDEBUG(DEBUG_AP, "%s: deauthentication: %pM len=%d, "
1773                "reason_code=%d\n", dev->name, hdr->addr2,
1774                len, reason_code);
1775
1776         spin_lock_bh(&local->ap->sta_table_lock);
1777         sta = ap_get_sta(local->ap, hdr->addr2);
1778         if (sta != NULL) {
1779                 if ((sta->flags & WLAN_STA_ASSOC) && !sta->ap)
1780                         hostap_event_expired_sta(local->dev, sta);
1781                 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
1782         }
1783         spin_unlock_bh(&local->ap->sta_table_lock);
1784         if (sta == NULL) {
1785                 printk("%s: deauthentication from %pM, "
1786                "reason_code=%d, but STA not authenticated\n", dev->name,
1787                        hdr->addr2, reason_code);
1788         }
1789 }
1790
1791
1792 /* Called only as a scheduled task for pending AP frames. */
1793 static void handle_disassoc(local_info_t *local, struct sk_buff *skb,
1794                             struct hostap_80211_rx_status *rx_stats)
1795 {
1796         struct net_device *dev = local->dev;
1797         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1798         char *body = skb->data + IEEE80211_MGMT_HDR_LEN;
1799         int len;
1800         u16 reason_code;
1801         __le16 *pos;
1802         struct sta_info *sta = NULL;
1803
1804         len = skb->len - IEEE80211_MGMT_HDR_LEN;
1805
1806         if (len < 2) {
1807                 printk("handle_disassoc - too short payload (len=%d)\n", len);
1808                 return;
1809         }
1810
1811         pos = (__le16 *) body;
1812         reason_code = le16_to_cpu(*pos);
1813
1814         PDEBUG(DEBUG_AP, "%s: disassociation: %pM len=%d, "
1815                "reason_code=%d\n", dev->name, hdr->addr2,
1816                len, reason_code);
1817
1818         spin_lock_bh(&local->ap->sta_table_lock);
1819         sta = ap_get_sta(local->ap, hdr->addr2);
1820         if (sta != NULL) {
1821                 if ((sta->flags & WLAN_STA_ASSOC) && !sta->ap)
1822                         hostap_event_expired_sta(local->dev, sta);
1823                 sta->flags &= ~WLAN_STA_ASSOC;
1824         }
1825         spin_unlock_bh(&local->ap->sta_table_lock);
1826         if (sta == NULL) {
1827                 printk("%s: disassociation from %pM, "
1828                        "reason_code=%d, but STA not authenticated\n",
1829                        dev->name, hdr->addr2, reason_code);
1830         }
1831 }
1832
1833
1834 /* Called only as a scheduled task for pending AP frames. */
1835 static void ap_handle_data_nullfunc(local_info_t *local,
1836                                     struct ieee80211_hdr *hdr)
1837 {
1838         struct net_device *dev = local->dev;
1839
1840         /* some STA f/w's seem to require control::ACK frame for
1841          * data::nullfunc, but at least Prism2 station f/w version 0.8.0 does
1842          * not send this..
1843          * send control::ACK for the data::nullfunc */
1844
1845         printk(KERN_DEBUG "Sending control::ACK for data::nullfunc\n");
1846         prism2_send_mgmt(dev, IEEE80211_FTYPE_CTL | IEEE80211_STYPE_ACK,
1847                          NULL, 0, hdr->addr2, 0);
1848 }
1849
1850
1851 /* Called only as a scheduled task for pending AP frames. */
1852 static void ap_handle_dropped_data(local_info_t *local,
1853                                    struct ieee80211_hdr *hdr)
1854 {
1855         struct net_device *dev = local->dev;
1856         struct sta_info *sta;
1857         __le16 reason;
1858
1859         spin_lock_bh(&local->ap->sta_table_lock);
1860         sta = ap_get_sta(local->ap, hdr->addr2);
1861         if (sta)
1862                 atomic_inc(&sta->users);
1863         spin_unlock_bh(&local->ap->sta_table_lock);
1864
1865         if (sta != NULL && (sta->flags & WLAN_STA_ASSOC)) {
1866                 PDEBUG(DEBUG_AP, "ap_handle_dropped_data: STA is now okay?\n");
1867                 atomic_dec(&sta->users);
1868                 return;
1869         }
1870
1871         reason = cpu_to_le16(WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1872         prism2_send_mgmt(dev, IEEE80211_FTYPE_MGMT |
1873                          ((sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) ?
1874                           IEEE80211_STYPE_DEAUTH : IEEE80211_STYPE_DISASSOC),
1875                          (char *) &reason, sizeof(reason), hdr->addr2, 0);
1876
1877         if (sta)
1878                 atomic_dec(&sta->users);
1879 }
1880
1881 #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
1882
1883
1884 /* Called only as a scheduled task for pending AP frames. */
1885 static void pspoll_send_buffered(local_info_t *local, struct sta_info *sta,
1886                                  struct sk_buff *skb)
1887 {
1888         struct hostap_skb_tx_data *meta;
1889
1890         if (!(sta->flags & WLAN_STA_PS)) {
1891                 /* Station has moved to non-PS mode, so send all buffered
1892                  * frames using normal device queue. */
1893                 dev_queue_xmit(skb);
1894                 return;
1895         }
1896
1897         /* add a flag for hostap_handle_sta_tx() to know that this skb should
1898          * be passed through even though STA is using PS */
1899         meta = (struct hostap_skb_tx_data *) skb->cb;
1900         meta->flags |= HOSTAP_TX_FLAGS_BUFFERED_FRAME;
1901         if (!skb_queue_empty(&sta->tx_buf)) {
1902                 /* indicate to STA that more frames follow */
1903                 meta->flags |= HOSTAP_TX_FLAGS_ADD_MOREDATA;
1904         }
1905         dev_queue_xmit(skb);
1906 }
1907
1908
1909 /* Called only as a scheduled task for pending AP frames. */
1910 static void handle_pspoll(local_info_t *local,
1911                           struct ieee80211_hdr *hdr,
1912                           struct hostap_80211_rx_status *rx_stats)
1913 {
1914         struct net_device *dev = local->dev;
1915         struct sta_info *sta;
1916         u16 aid;
1917         struct sk_buff *skb;
1918
1919         PDEBUG(DEBUG_PS2, "handle_pspoll: BSSID=%pM, TA=%pM PWRMGT=%d\n",
1920                hdr->addr1, hdr->addr2, !!ieee80211_has_pm(hdr->frame_control));
1921
1922         if (!ether_addr_equal(hdr->addr1, dev->dev_addr)) {
1923                 PDEBUG(DEBUG_AP,
1924                        "handle_pspoll - addr1(BSSID)=%pM not own MAC\n",
1925                        hdr->addr1);
1926                 return;
1927         }
1928
1929         aid = le16_to_cpu(hdr->duration_id);
1930         if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14))) {
1931                 PDEBUG(DEBUG_PS, "   PSPOLL and AID[15:14] not set\n");
1932                 return;
1933         }
1934         aid &= ~(BIT(15) | BIT(14));
1935         if (aid == 0 || aid > MAX_AID_TABLE_SIZE) {
1936                 PDEBUG(DEBUG_PS, "   invalid aid=%d\n", aid);
1937                 return;
1938         }
1939         PDEBUG(DEBUG_PS2, "   aid=%d\n", aid);
1940
1941         spin_lock_bh(&local->ap->sta_table_lock);
1942         sta = ap_get_sta(local->ap, hdr->addr2);
1943         if (sta)
1944                 atomic_inc(&sta->users);
1945         spin_unlock_bh(&local->ap->sta_table_lock);
1946
1947         if (sta == NULL) {
1948                 PDEBUG(DEBUG_PS, "   STA not found\n");
1949                 return;
1950         }
1951         if (sta->aid != aid) {
1952                 PDEBUG(DEBUG_PS, "   received aid=%i does not match with "
1953                        "assoc.aid=%d\n", aid, sta->aid);
1954                 return;
1955         }
1956
1957         /* FIX: todo:
1958          * - add timeout for buffering (clear aid in TIM vector if buffer timed
1959          *   out (expiry time must be longer than ListenInterval for
1960          *   the corresponding STA; "8802-11: 11.2.1.9 AP aging function"
1961          * - what to do, if buffered, pspolled, and sent frame is not ACKed by
1962          *   sta; store buffer for later use and leave TIM aid bit set? use
1963          *   TX event to check whether frame was ACKed?
1964          */
1965
1966         while ((skb = skb_dequeue(&sta->tx_buf)) != NULL) {
1967                 /* send buffered frame .. */
1968                 PDEBUG(DEBUG_PS2, "Sending buffered frame to STA after PS POLL"
1969                        " (buffer_count=%d)\n", skb_queue_len(&sta->tx_buf));
1970
1971                 pspoll_send_buffered(local, sta, skb);
1972
1973                 if (sta->flags & WLAN_STA_PS) {
1974                         /* send only one buffered packet per PS Poll */
1975                         /* FIX: should ignore further PS Polls until the
1976                          * buffered packet that was just sent is acknowledged
1977                          * (Tx or TxExc event) */
1978                         break;
1979                 }
1980         }
1981
1982         if (skb_queue_empty(&sta->tx_buf)) {
1983                 /* try to clear aid from TIM */
1984                 if (!(sta->flags & WLAN_STA_TIM))
1985                         PDEBUG(DEBUG_PS2,  "Re-unsetting TIM for aid %d\n",
1986                                aid);
1987                 hostap_set_tim(local, aid, 0);
1988                 sta->flags &= ~WLAN_STA_TIM;
1989         }
1990
1991         atomic_dec(&sta->users);
1992 }
1993
1994
1995 #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
1996
1997 static void handle_wds_oper_queue(struct work_struct *work)
1998 {
1999         struct ap_data *ap = container_of(work, struct ap_data,
2000                                           wds_oper_queue);
2001         local_info_t *local = ap->local;
2002         struct wds_oper_data *entry, *prev;
2003
2004         spin_lock_bh(&local->lock);
2005         entry = local->ap->wds_oper_entries;
2006         local->ap->wds_oper_entries = NULL;
2007         spin_unlock_bh(&local->lock);
2008
2009         while (entry) {
2010                 PDEBUG(DEBUG_AP, "%s: %s automatic WDS connection "
2011                        "to AP %pM\n",
2012                        local->dev->name,
2013                        entry->type == WDS_ADD ? "adding" : "removing",
2014                        entry->addr);
2015                 if (entry->type == WDS_ADD)
2016                         prism2_wds_add(local, entry->addr, 0);
2017                 else if (entry->type == WDS_DEL)
2018                         prism2_wds_del(local, entry->addr, 0, 1);
2019
2020                 prev = entry;
2021                 entry = entry->next;
2022                 kfree(prev);
2023         }
2024 }
2025
2026
2027 /* Called only as a scheduled task for pending AP frames. */
2028 static void handle_beacon(local_info_t *local, struct sk_buff *skb,
2029                           struct hostap_80211_rx_status *rx_stats)
2030 {
2031         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
2032         char *body = skb->data + IEEE80211_MGMT_HDR_LEN;
2033         int len, left;
2034         u16 beacon_int, capability;
2035         __le16 *pos;
2036         char *ssid = NULL;
2037         unsigned char *supp_rates = NULL;
2038         int ssid_len = 0, supp_rates_len = 0;
2039         struct sta_info *sta = NULL;
2040         int new_sta = 0, channel = -1;
2041
2042         len = skb->len - IEEE80211_MGMT_HDR_LEN;
2043
2044         if (len < 8 + 2 + 2) {
2045                 printk(KERN_DEBUG "handle_beacon - too short payload "
2046                        "(len=%d)\n", len);
2047                 return;
2048         }
2049
2050         pos = (__le16 *) body;
2051         left = len;
2052
2053         /* Timestamp (8 octets) */
2054         pos += 4; left -= 8;
2055         /* Beacon interval (2 octets) */
2056         beacon_int = le16_to_cpu(*pos);
2057         pos++; left -= 2;
2058         /* Capability information (2 octets) */
2059         capability = le16_to_cpu(*pos);
2060         pos++; left -= 2;
2061
2062         if (local->ap->ap_policy != AP_OTHER_AP_EVEN_IBSS &&
2063             capability & WLAN_CAPABILITY_IBSS)
2064                 return;
2065
2066         if (left >= 2) {
2067                 unsigned int ileft;
2068                 unsigned char *u = (unsigned char *) pos;
2069
2070                 if (*u == WLAN_EID_SSID) {
2071                         u++; left--;
2072                         ileft = *u;
2073                         u++; left--;
2074
2075                         if (ileft > left || ileft > MAX_SSID_LEN) {
2076                                 PDEBUG(DEBUG_AP, "SSID: overflow\n");
2077                                 return;
2078                         }
2079
2080                         if (local->ap->ap_policy == AP_OTHER_AP_SAME_SSID &&
2081                             (ileft != strlen(local->essid) ||
2082                              memcmp(local->essid, u, ileft) != 0)) {
2083                                 /* not our SSID */
2084                                 return;
2085                         }
2086
2087                         ssid = u;
2088                         ssid_len = ileft;
2089
2090                         u += ileft;
2091                         left -= ileft;
2092                 }
2093
2094                 if (*u == WLAN_EID_SUPP_RATES) {
2095                         u++; left--;
2096                         ileft = *u;
2097                         u++; left--;
2098
2099                         if (ileft > left || ileft == 0 || ileft > 8) {
2100                                 PDEBUG(DEBUG_AP, " - SUPP_RATES len error\n");
2101                                 return;
2102                         }
2103
2104                         supp_rates = u;
2105                         supp_rates_len = ileft;
2106
2107                         u += ileft;
2108                         left -= ileft;
2109                 }
2110
2111                 if (*u == WLAN_EID_DS_PARAMS) {
2112                         u++; left--;
2113                         ileft = *u;
2114                         u++; left--;
2115
2116                         if (ileft > left || ileft != 1) {
2117                                 PDEBUG(DEBUG_AP, " - DS_PARAMS len error\n");
2118                                 return;
2119                         }
2120
2121                         channel = *u;
2122
2123                         u += ileft;
2124                         left -= ileft;
2125                 }
2126         }
2127
2128         spin_lock_bh(&local->ap->sta_table_lock);
2129         sta = ap_get_sta(local->ap, hdr->addr2);
2130         if (sta != NULL)
2131                 atomic_inc(&sta->users);
2132         spin_unlock_bh(&local->ap->sta_table_lock);
2133
2134         if (sta == NULL) {
2135                 /* add new AP */
2136                 new_sta = 1;
2137                 sta = ap_add_sta(local->ap, hdr->addr2);
2138                 if (sta == NULL) {
2139                         printk(KERN_INFO "prism2: kmalloc failed for AP "
2140                                "data structure\n");
2141                         return;
2142                 }
2143                 hostap_event_new_sta(local->dev, sta);
2144
2145                 /* mark APs authentication and associated for pseudo ad-hoc
2146                  * style communication */
2147                 sta->flags = WLAN_STA_AUTH | WLAN_STA_ASSOC;
2148
2149                 if (local->ap->autom_ap_wds) {
2150                         hostap_wds_link_oper(local, sta->addr, WDS_ADD);
2151                 }
2152         }
2153
2154         sta->ap = 1;
2155         if (ssid) {
2156                 sta->u.ap.ssid_len = ssid_len;
2157                 memcpy(sta->u.ap.ssid, ssid, ssid_len);
2158                 sta->u.ap.ssid[ssid_len] = '\0';
2159         } else {
2160                 sta->u.ap.ssid_len = 0;
2161                 sta->u.ap.ssid[0] = '\0';
2162         }
2163         sta->u.ap.channel = channel;
2164         sta->rx_packets++;
2165         sta->rx_bytes += len;
2166         sta->u.ap.last_beacon = sta->last_rx = jiffies;
2167         sta->capability = capability;
2168         sta->listen_interval = beacon_int;
2169
2170         atomic_dec(&sta->users);
2171
2172         if (new_sta) {
2173                 memset(sta->supported_rates, 0, sizeof(sta->supported_rates));
2174                 memcpy(sta->supported_rates, supp_rates, supp_rates_len);
2175                 prism2_check_tx_rates(sta);
2176         }
2177 }
2178
2179 #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
2180
2181
2182 /* Called only as a tasklet. */
2183 static void handle_ap_item(local_info_t *local, struct sk_buff *skb,
2184                            struct hostap_80211_rx_status *rx_stats)
2185 {
2186 #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
2187         struct net_device *dev = local->dev;
2188 #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
2189         u16 fc, type, stype;
2190         struct ieee80211_hdr *hdr;
2191
2192         /* FIX: should give skb->len to handler functions and check that the
2193          * buffer is long enough */
2194         hdr = (struct ieee80211_hdr *) skb->data;
2195         fc = le16_to_cpu(hdr->frame_control);
2196         type = fc & IEEE80211_FCTL_FTYPE;
2197         stype = fc & IEEE80211_FCTL_STYPE;
2198
2199 #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
2200         if (!local->hostapd && type == IEEE80211_FTYPE_DATA) {
2201                 PDEBUG(DEBUG_AP, "handle_ap_item - data frame\n");
2202
2203                 if (!(fc & IEEE80211_FCTL_TODS) ||
2204                     (fc & IEEE80211_FCTL_FROMDS)) {
2205                         if (stype == IEEE80211_STYPE_NULLFUNC) {
2206                                 /* no ToDS nullfunc seems to be used to check
2207                                  * AP association; so send reject message to
2208                                  * speed up re-association */
2209                                 ap_handle_dropped_data(local, hdr);
2210                                 goto done;
2211                         }
2212                         PDEBUG(DEBUG_AP, "   not ToDS frame (fc=0x%04x)\n",
2213                                fc);
2214                         goto done;
2215                 }
2216
2217                 if (!ether_addr_equal(hdr->addr1, dev->dev_addr)) {
2218                         PDEBUG(DEBUG_AP, "handle_ap_item - addr1(BSSID)=%pM"
2219                                " not own MAC\n", hdr->addr1);
2220                         goto done;
2221                 }
2222
2223                 if (local->ap->nullfunc_ack &&
2224                     stype == IEEE80211_STYPE_NULLFUNC)
2225                         ap_handle_data_nullfunc(local, hdr);
2226                 else
2227                         ap_handle_dropped_data(local, hdr);
2228                 goto done;
2229         }
2230
2231         if (type == IEEE80211_FTYPE_MGMT && stype == IEEE80211_STYPE_BEACON) {
2232                 handle_beacon(local, skb, rx_stats);
2233                 goto done;
2234         }
2235 #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
2236
2237         if (type == IEEE80211_FTYPE_CTL && stype == IEEE80211_STYPE_PSPOLL) {
2238                 handle_pspoll(local, hdr, rx_stats);
2239                 goto done;
2240         }
2241
2242         if (local->hostapd) {
2243                 PDEBUG(DEBUG_AP, "Unknown frame in AP queue: type=0x%02x "
2244                        "subtype=0x%02x\n", type, stype);
2245                 goto done;
2246         }
2247
2248 #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
2249         if (type != IEEE80211_FTYPE_MGMT) {
2250                 PDEBUG(DEBUG_AP, "handle_ap_item - not a management frame?\n");
2251                 goto done;
2252         }
2253
2254         if (!ether_addr_equal(hdr->addr1, dev->dev_addr)) {
2255                 PDEBUG(DEBUG_AP, "handle_ap_item - addr1(DA)=%pM"
2256                        " not own MAC\n", hdr->addr1);
2257                 goto done;
2258         }
2259
2260         if (!ether_addr_equal(hdr->addr3, dev->dev_addr)) {
2261                 PDEBUG(DEBUG_AP, "handle_ap_item - addr3(BSSID)=%pM"
2262                        " not own MAC\n", hdr->addr3);
2263                 goto done;
2264         }
2265
2266         switch (stype) {
2267         case IEEE80211_STYPE_ASSOC_REQ:
2268                 handle_assoc(local, skb, rx_stats, 0);
2269                 break;
2270         case IEEE80211_STYPE_ASSOC_RESP:
2271                 PDEBUG(DEBUG_AP, "==> ASSOC RESP (ignored)\n");
2272                 break;
2273         case IEEE80211_STYPE_REASSOC_REQ:
2274                 handle_assoc(local, skb, rx_stats, 1);
2275                 break;
2276         case IEEE80211_STYPE_REASSOC_RESP:
2277                 PDEBUG(DEBUG_AP, "==> REASSOC RESP (ignored)\n");
2278                 break;
2279         case IEEE80211_STYPE_ATIM:
2280                 PDEBUG(DEBUG_AP, "==> ATIM (ignored)\n");
2281                 break;
2282         case IEEE80211_STYPE_DISASSOC:
2283                 handle_disassoc(local, skb, rx_stats);
2284                 break;
2285         case IEEE80211_STYPE_AUTH:
2286                 handle_authen(local, skb, rx_stats);
2287                 break;
2288         case IEEE80211_STYPE_DEAUTH:
2289                 handle_deauth(local, skb, rx_stats);
2290                 break;
2291         default:
2292                 PDEBUG(DEBUG_AP, "Unknown mgmt frame subtype 0x%02x\n",
2293                        stype >> 4);
2294                 break;
2295         }
2296 #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
2297
2298  done:
2299         dev_kfree_skb(skb);
2300 }
2301
2302
2303 /* Called only as a tasklet (software IRQ) */
2304 void hostap_rx(struct net_device *dev, struct sk_buff *skb,
2305                struct hostap_80211_rx_status *rx_stats)
2306 {
2307         struct hostap_interface *iface;
2308         local_info_t *local;
2309         struct ieee80211_hdr *hdr;
2310
2311         iface = netdev_priv(dev);
2312         local = iface->local;
2313
2314         if (skb->len < 16)
2315                 goto drop;
2316
2317         dev->stats.rx_packets++;
2318
2319         hdr = (struct ieee80211_hdr *) skb->data;
2320
2321         if (local->ap->ap_policy == AP_OTHER_AP_SKIP_ALL &&
2322             ieee80211_is_beacon(hdr->frame_control))
2323                 goto drop;
2324
2325         skb->protocol = cpu_to_be16(ETH_P_HOSTAP);
2326         handle_ap_item(local, skb, rx_stats);
2327         return;
2328
2329  drop:
2330         dev_kfree_skb(skb);
2331 }
2332
2333
2334 /* Called only as a tasklet (software IRQ) */
2335 static void schedule_packet_send(local_info_t *local, struct sta_info *sta)
2336 {
2337         struct sk_buff *skb;
2338         struct ieee80211_hdr *hdr;
2339         struct hostap_80211_rx_status rx_stats;
2340
2341         if (skb_queue_empty(&sta->tx_buf))
2342                 return;
2343
2344         skb = dev_alloc_skb(16);
2345         if (skb == NULL) {
2346                 printk(KERN_DEBUG "%s: schedule_packet_send: skb alloc "
2347                        "failed\n", local->dev->name);
2348                 return;
2349         }
2350
2351         hdr = skb_put(skb, 16);
2352
2353         /* Generate a fake pspoll frame to start packet delivery */
2354         hdr->frame_control = cpu_to_le16(
2355                 IEEE80211_FTYPE_CTL | IEEE80211_STYPE_PSPOLL);
2356         memcpy(hdr->addr1, local->dev->dev_addr, ETH_ALEN);
2357         memcpy(hdr->addr2, sta->addr, ETH_ALEN);
2358         hdr->duration_id = cpu_to_le16(sta->aid | BIT(15) | BIT(14));
2359
2360         PDEBUG(DEBUG_PS2,
2361                "%s: Scheduling buffered packet delivery for STA %pM\n",
2362                local->dev->name, sta->addr);
2363
2364         skb->dev = local->dev;
2365
2366         memset(&rx_stats, 0, sizeof(rx_stats));
2367         hostap_rx(local->dev, skb, &rx_stats);
2368 }
2369
2370
2371 int prism2_ap_get_sta_qual(local_info_t *local, struct sockaddr addr[],
2372                            struct iw_quality qual[], int buf_size,
2373                            int aplist)
2374 {
2375         struct ap_data *ap = local->ap;
2376         struct list_head *ptr;
2377         int count = 0;
2378
2379         spin_lock_bh(&ap->sta_table_lock);
2380
2381         for (ptr = ap->sta_list.next; ptr != NULL && ptr != &ap->sta_list;
2382              ptr = ptr->next) {
2383                 struct sta_info *sta = (struct sta_info *) ptr;
2384
2385                 if (aplist && !sta->ap)
2386                         continue;
2387                 addr[count].sa_family = ARPHRD_ETHER;
2388                 memcpy(addr[count].sa_data, sta->addr, ETH_ALEN);
2389                 if (sta->last_rx_silence == 0)
2390                         qual[count].qual = sta->last_rx_signal < 27 ?
2391                                 0 : (sta->last_rx_signal - 27) * 92 / 127;
2392                 else
2393                         qual[count].qual = sta->last_rx_signal -
2394                                 sta->last_rx_silence - 35;
2395                 qual[count].level = HFA384X_LEVEL_TO_dBm(sta->last_rx_signal);
2396                 qual[count].noise = HFA384X_LEVEL_TO_dBm(sta->last_rx_silence);
2397                 qual[count].updated = sta->last_rx_updated;
2398
2399                 sta->last_rx_updated = IW_QUAL_DBM;
2400
2401                 count++;
2402                 if (count >= buf_size)
2403                         break;
2404         }
2405         spin_unlock_bh(&ap->sta_table_lock);
2406
2407         return count;
2408 }
2409
2410
2411 /* Translate our list of Access Points & Stations to a card independent
2412  * format that the Wireless Tools will understand - Jean II */
2413 int prism2_ap_translate_scan(struct net_device *dev,
2414                              struct iw_request_info *info, char *buffer)
2415 {
2416         struct hostap_interface *iface;
2417         local_info_t *local;
2418         struct ap_data *ap;
2419         struct list_head *ptr;
2420         struct iw_event iwe;
2421         char *current_ev = buffer;
2422         char *end_buf = buffer + IW_SCAN_MAX_DATA;
2423 #if !defined(PRISM2_NO_KERNEL_IEEE80211_MGMT)
2424         char buf[64];
2425 #endif
2426
2427         iface = netdev_priv(dev);
2428         local = iface->local;
2429         ap = local->ap;
2430
2431         spin_lock_bh(&ap->sta_table_lock);
2432
2433         for (ptr = ap->sta_list.next; ptr != NULL && ptr != &ap->sta_list;
2434              ptr = ptr->next) {
2435                 struct sta_info *sta = (struct sta_info *) ptr;
2436
2437                 /* First entry *MUST* be the AP MAC address */
2438                 memset(&iwe, 0, sizeof(iwe));
2439                 iwe.cmd = SIOCGIWAP;
2440                 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
2441                 memcpy(iwe.u.ap_addr.sa_data, sta->addr, ETH_ALEN);
2442                 iwe.len = IW_EV_ADDR_LEN;
2443                 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
2444                                                   &iwe, IW_EV_ADDR_LEN);
2445
2446                 /* Use the mode to indicate if it's a station or
2447                  * an Access Point */
2448                 memset(&iwe, 0, sizeof(iwe));
2449                 iwe.cmd = SIOCGIWMODE;
2450                 if (sta->ap)
2451                         iwe.u.mode = IW_MODE_MASTER;
2452                 else
2453                         iwe.u.mode = IW_MODE_INFRA;
2454                 iwe.len = IW_EV_UINT_LEN;
2455                 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
2456                                                   &iwe, IW_EV_UINT_LEN);
2457
2458                 /* Some quality */
2459                 memset(&iwe, 0, sizeof(iwe));
2460                 iwe.cmd = IWEVQUAL;
2461                 if (sta->last_rx_silence == 0)
2462                         iwe.u.qual.qual = sta->last_rx_signal < 27 ?
2463                                 0 : (sta->last_rx_signal - 27) * 92 / 127;
2464                 else
2465                         iwe.u.qual.qual = sta->last_rx_signal -
2466                                 sta->last_rx_silence - 35;
2467                 iwe.u.qual.level = HFA384X_LEVEL_TO_dBm(sta->last_rx_signal);
2468                 iwe.u.qual.noise = HFA384X_LEVEL_TO_dBm(sta->last_rx_silence);
2469                 iwe.u.qual.updated = sta->last_rx_updated;
2470                 iwe.len = IW_EV_QUAL_LEN;
2471                 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
2472                                                   &iwe, IW_EV_QUAL_LEN);
2473
2474 #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
2475                 if (sta->ap) {
2476                         memset(&iwe, 0, sizeof(iwe));
2477                         iwe.cmd = SIOCGIWESSID;
2478                         iwe.u.data.length = sta->u.ap.ssid_len;
2479                         iwe.u.data.flags = 1;
2480                         current_ev = iwe_stream_add_point(info, current_ev,
2481                                                           end_buf, &iwe,
2482                                                           sta->u.ap.ssid);
2483
2484                         memset(&iwe, 0, sizeof(iwe));
2485                         iwe.cmd = SIOCGIWENCODE;
2486                         if (sta->capability & WLAN_CAPABILITY_PRIVACY)
2487                                 iwe.u.data.flags =
2488                                         IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
2489                         else
2490                                 iwe.u.data.flags = IW_ENCODE_DISABLED;
2491                         current_ev = iwe_stream_add_point(info, current_ev,
2492                                                           end_buf, &iwe,
2493                                                           sta->u.ap.ssid);
2494
2495                         if (sta->u.ap.channel > 0 &&
2496                             sta->u.ap.channel <= FREQ_COUNT) {
2497                                 memset(&iwe, 0, sizeof(iwe));
2498                                 iwe.cmd = SIOCGIWFREQ;
2499                                 iwe.u.freq.m = freq_list[sta->u.ap.channel - 1]
2500                                         * 100000;
2501                                 iwe.u.freq.e = 1;
2502                                 current_ev = iwe_stream_add_event(
2503                                         info, current_ev, end_buf, &iwe,
2504                                         IW_EV_FREQ_LEN);
2505                         }
2506
2507                         memset(&iwe, 0, sizeof(iwe));
2508                         iwe.cmd = IWEVCUSTOM;
2509                         sprintf(buf, "beacon_interval=%d",
2510                                 sta->listen_interval);
2511                         iwe.u.data.length = strlen(buf);
2512                         current_ev = iwe_stream_add_point(info, current_ev,
2513                                                           end_buf, &iwe, buf);
2514                 }
2515 #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
2516
2517                 sta->last_rx_updated = IW_QUAL_DBM;
2518
2519                 /* To be continued, we should make good use of IWEVCUSTOM */
2520         }
2521
2522         spin_unlock_bh(&ap->sta_table_lock);
2523
2524         return current_ev - buffer;
2525 }
2526
2527
2528 static int prism2_hostapd_add_sta(struct ap_data *ap,
2529                                   struct prism2_hostapd_param *param)
2530 {
2531         struct sta_info *sta;
2532
2533         spin_lock_bh(&ap->sta_table_lock);
2534         sta = ap_get_sta(ap, param->sta_addr);
2535         if (sta)
2536                 atomic_inc(&sta->users);
2537         spin_unlock_bh(&ap->sta_table_lock);
2538
2539         if (sta == NULL) {
2540                 sta = ap_add_sta(ap, param->sta_addr);
2541                 if (sta == NULL)
2542                         return -1;
2543         }
2544
2545         if (!(sta->flags & WLAN_STA_ASSOC) && !sta->ap && sta->local)
2546                 hostap_event_new_sta(sta->local->dev, sta);
2547
2548         sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
2549         sta->last_rx = jiffies;
2550         sta->aid = param->u.add_sta.aid;
2551         sta->capability = param->u.add_sta.capability;
2552         sta->tx_supp_rates = param->u.add_sta.tx_supp_rates;
2553         if (sta->tx_supp_rates & WLAN_RATE_1M)
2554                 sta->supported_rates[0] = 2;
2555         if (sta->tx_supp_rates & WLAN_RATE_2M)
2556                 sta->supported_rates[1] = 4;
2557         if (sta->tx_supp_rates & WLAN_RATE_5M5)
2558                 sta->supported_rates[2] = 11;
2559         if (sta->tx_supp_rates & WLAN_RATE_11M)
2560                 sta->supported_rates[3] = 22;
2561         prism2_check_tx_rates(sta);
2562         atomic_dec(&sta->users);
2563         return 0;
2564 }
2565
2566
2567 static int prism2_hostapd_remove_sta(struct ap_data *ap,
2568                                      struct prism2_hostapd_param *param)
2569 {
2570         struct sta_info *sta;
2571
2572         spin_lock_bh(&ap->sta_table_lock);
2573         sta = ap_get_sta(ap, param->sta_addr);
2574         if (sta) {
2575                 ap_sta_hash_del(ap, sta);
2576                 list_del(&sta->list);
2577         }
2578         spin_unlock_bh(&ap->sta_table_lock);
2579
2580         if (!sta)
2581                 return -ENOENT;
2582
2583         if ((sta->flags & WLAN_STA_ASSOC) && !sta->ap && sta->local)
2584                 hostap_event_expired_sta(sta->local->dev, sta);
2585         ap_free_sta(ap, sta);
2586
2587         return 0;
2588 }
2589
2590
2591 static int prism2_hostapd_get_info_sta(struct ap_data *ap,
2592                                        struct prism2_hostapd_param *param)
2593 {
2594         struct sta_info *sta;
2595
2596         spin_lock_bh(&ap->sta_table_lock);
2597         sta = ap_get_sta(ap, param->sta_addr);
2598         if (sta)
2599                 atomic_inc(&sta->users);
2600         spin_unlock_bh(&ap->sta_table_lock);
2601
2602         if (!sta)
2603                 return -ENOENT;
2604
2605         param->u.get_info_sta.inactive_sec = (jiffies - sta->last_rx) / HZ;
2606
2607         atomic_dec(&sta->users);
2608
2609         return 1;
2610 }
2611
2612
2613 static int prism2_hostapd_set_flags_sta(struct ap_data *ap,
2614                                         struct prism2_hostapd_param *param)
2615 {
2616         struct sta_info *sta;
2617
2618         spin_lock_bh(&ap->sta_table_lock);
2619         sta = ap_get_sta(ap, param->sta_addr);
2620         if (sta) {
2621                 sta->flags |= param->u.set_flags_sta.flags_or;
2622                 sta->flags &= param->u.set_flags_sta.flags_and;
2623         }
2624         spin_unlock_bh(&ap->sta_table_lock);
2625
2626         if (!sta)
2627                 return -ENOENT;
2628
2629         return 0;
2630 }
2631
2632
2633 static int prism2_hostapd_sta_clear_stats(struct ap_data *ap,
2634                                           struct prism2_hostapd_param *param)
2635 {
2636         struct sta_info *sta;
2637         int rate;
2638
2639         spin_lock_bh(&ap->sta_table_lock);
2640         sta = ap_get_sta(ap, param->sta_addr);
2641         if (sta) {
2642                 sta->rx_packets = sta->tx_packets = 0;
2643                 sta->rx_bytes = sta->tx_bytes = 0;
2644                 for (rate = 0; rate < WLAN_RATE_COUNT; rate++) {
2645                         sta->tx_count[rate] = 0;
2646                         sta->rx_count[rate] = 0;
2647                 }
2648         }
2649         spin_unlock_bh(&ap->sta_table_lock);
2650
2651         if (!sta)
2652                 return -ENOENT;
2653
2654         return 0;
2655 }
2656
2657
2658 int prism2_hostapd(struct ap_data *ap, struct prism2_hostapd_param *param)
2659 {
2660         switch (param->cmd) {
2661         case PRISM2_HOSTAPD_FLUSH:
2662                 ap_control_kickall(ap);
2663                 return 0;
2664         case PRISM2_HOSTAPD_ADD_STA:
2665                 return prism2_hostapd_add_sta(ap, param);
2666         case PRISM2_HOSTAPD_REMOVE_STA:
2667                 return prism2_hostapd_remove_sta(ap, param);
2668         case PRISM2_HOSTAPD_GET_INFO_STA:
2669                 return prism2_hostapd_get_info_sta(ap, param);
2670         case PRISM2_HOSTAPD_SET_FLAGS_STA:
2671                 return prism2_hostapd_set_flags_sta(ap, param);
2672         case PRISM2_HOSTAPD_STA_CLEAR_STATS:
2673                 return prism2_hostapd_sta_clear_stats(ap, param);
2674         default:
2675                 printk(KERN_WARNING "prism2_hostapd: unknown cmd=%d\n",
2676                        param->cmd);
2677                 return -EOPNOTSUPP;
2678         }
2679 }
2680
2681
2682 /* Update station info for host-based TX rate control and return current
2683  * TX rate */
2684 static int ap_update_sta_tx_rate(struct sta_info *sta, struct net_device *dev)
2685 {
2686         int ret = sta->tx_rate;
2687         struct hostap_interface *iface;
2688         local_info_t *local;
2689
2690         iface = netdev_priv(dev);
2691         local = iface->local;
2692
2693         sta->tx_count[sta->tx_rate_idx]++;
2694         sta->tx_since_last_failure++;
2695         sta->tx_consecutive_exc = 0;
2696         if (sta->tx_since_last_failure >= WLAN_RATE_UPDATE_COUNT &&
2697             sta->tx_rate_idx < sta->tx_max_rate) {
2698                 /* use next higher rate */
2699                 int old_rate, new_rate;
2700                 old_rate = new_rate = sta->tx_rate_idx;
2701                 while (new_rate < sta->tx_max_rate) {
2702                         new_rate++;
2703                         if (ap_tx_rate_ok(new_rate, sta, local)) {
2704                                 sta->tx_rate_idx = new_rate;
2705                                 break;
2706                         }
2707                 }
2708                 if (old_rate != sta->tx_rate_idx) {
2709                         switch (sta->tx_rate_idx) {
2710                         case 0: sta->tx_rate = 10; break;
2711                         case 1: sta->tx_rate = 20; break;
2712                         case 2: sta->tx_rate = 55; break;
2713                         case 3: sta->tx_rate = 110; break;
2714                         default: sta->tx_rate = 0; break;
2715                         }
2716                         PDEBUG(DEBUG_AP, "%s: STA %pM TX rate raised to %d\n",
2717                                dev->name, sta->addr, sta->tx_rate);
2718                 }
2719                 sta->tx_since_last_failure = 0;
2720         }
2721
2722         return ret;
2723 }
2724
2725
2726 /* Called only from software IRQ. Called for each TX frame prior possible
2727  * encryption and transmit. */
2728 ap_tx_ret hostap_handle_sta_tx(local_info_t *local, struct hostap_tx_data *tx)
2729 {
2730         struct sta_info *sta = NULL;
2731         struct sk_buff *skb = tx->skb;
2732         int set_tim, ret;
2733         struct ieee80211_hdr *hdr;
2734         struct hostap_skb_tx_data *meta;
2735
2736         meta = (struct hostap_skb_tx_data *) skb->cb;
2737         ret = AP_TX_CONTINUE;
2738         if (local->ap == NULL || skb->len < 10 ||
2739             meta->iface->type == HOSTAP_INTERFACE_STA)
2740                 goto out;
2741
2742         hdr = (struct ieee80211_hdr *) skb->data;
2743
2744         if (hdr->addr1[0] & 0x01) {
2745                 /* broadcast/multicast frame - no AP related processing */
2746                 if (local->ap->num_sta <= 0)
2747                         ret = AP_TX_DROP;
2748                 goto out;
2749         }
2750
2751         /* unicast packet - check whether destination STA is associated */
2752         spin_lock(&local->ap->sta_table_lock);
2753         sta = ap_get_sta(local->ap, hdr->addr1);
2754         if (sta)
2755                 atomic_inc(&sta->users);
2756         spin_unlock(&local->ap->sta_table_lock);
2757
2758         if (local->iw_mode == IW_MODE_MASTER && sta == NULL &&
2759             !(meta->flags & HOSTAP_TX_FLAGS_WDS) &&
2760             meta->iface->type != HOSTAP_INTERFACE_MASTER &&
2761             meta->iface->type != HOSTAP_INTERFACE_AP) {
2762 #if 0
2763                 /* This can happen, e.g., when wlan0 is added to a bridge and
2764                  * bridging code does not know which port is the correct target
2765                  * for a unicast frame. In this case, the packet is send to all
2766                  * ports of the bridge. Since this is a valid scenario, do not
2767                  * print out any errors here. */
2768                 if (net_ratelimit()) {
2769                         printk(KERN_DEBUG "AP: drop packet to non-associated "
2770                                "STA %pM\n", hdr->addr1);
2771                 }
2772 #endif
2773                 local->ap->tx_drop_nonassoc++;
2774                 ret = AP_TX_DROP;
2775                 goto out;
2776         }
2777
2778         if (sta == NULL)
2779                 goto out;
2780
2781         if (!(sta->flags & WLAN_STA_AUTHORIZED))
2782                 ret = AP_TX_CONTINUE_NOT_AUTHORIZED;
2783
2784         /* Set tx_rate if using host-based TX rate control */
2785         if (!local->fw_tx_rate_control)
2786                 local->ap->last_tx_rate = meta->rate =
2787                         ap_update_sta_tx_rate(sta, local->dev);
2788
2789         if (local->iw_mode != IW_MODE_MASTER)
2790                 goto out;
2791
2792         if (!(sta->flags & WLAN_STA_PS))
2793                 goto out;
2794
2795         if (meta->flags & HOSTAP_TX_FLAGS_ADD_MOREDATA) {
2796                 /* indicate to STA that more frames follow */
2797                 hdr->frame_control |=
2798                         cpu_to_le16(IEEE80211_FCTL_MOREDATA);
2799         }
2800
2801         if (meta->flags & HOSTAP_TX_FLAGS_BUFFERED_FRAME) {
2802                 /* packet was already buffered and now send due to
2803                  * PS poll, so do not rebuffer it */
2804                 goto out;
2805         }
2806
2807         if (skb_queue_len(&sta->tx_buf) >= STA_MAX_TX_BUFFER) {
2808                 PDEBUG(DEBUG_PS, "%s: No more space in STA (%pM)'s"
2809                        "PS mode buffer\n",
2810                        local->dev->name, sta->addr);
2811                 /* Make sure that TIM is set for the station (it might not be
2812                  * after AP wlan hw reset). */
2813                 /* FIX: should fix hw reset to restore bits based on STA
2814                  * buffer state.. */
2815                 hostap_set_tim(local, sta->aid, 1);
2816                 sta->flags |= WLAN_STA_TIM;
2817                 ret = AP_TX_DROP;
2818                 goto out;
2819         }
2820
2821         /* STA in PS mode, buffer frame for later delivery */
2822         set_tim = skb_queue_empty(&sta->tx_buf);
2823         skb_queue_tail(&sta->tx_buf, skb);
2824         /* FIX: could save RX time to skb and expire buffered frames after
2825          * some time if STA does not poll for them */
2826
2827         if (set_tim) {
2828                 if (sta->flags & WLAN_STA_TIM)
2829                         PDEBUG(DEBUG_PS2, "Re-setting TIM for aid %d\n",
2830                                sta->aid);
2831                 hostap_set_tim(local, sta->aid, 1);
2832                 sta->flags |= WLAN_STA_TIM;
2833         }
2834
2835         ret = AP_TX_BUFFERED;
2836
2837  out:
2838         if (sta != NULL) {
2839                 if (ret == AP_TX_CONTINUE ||
2840                     ret == AP_TX_CONTINUE_NOT_AUTHORIZED) {
2841                         sta->tx_packets++;
2842                         sta->tx_bytes += skb->len;
2843                         sta->last_tx = jiffies;
2844                 }
2845
2846                 if ((ret == AP_TX_CONTINUE ||
2847                      ret == AP_TX_CONTINUE_NOT_AUTHORIZED) &&
2848                     sta->crypt && tx->host_encrypt) {
2849                         tx->crypt = sta->crypt;
2850                         tx->sta_ptr = sta; /* hostap_handle_sta_release() will
2851                                             * be called to release sta info
2852                                             * later */
2853                 } else
2854                         atomic_dec(&sta->users);
2855         }
2856
2857         return ret;
2858 }
2859
2860
2861 void hostap_handle_sta_release(void *ptr)
2862 {
2863         struct sta_info *sta = ptr;
2864         atomic_dec(&sta->users);
2865 }
2866
2867
2868 /* Called only as a tasklet (software IRQ) */
2869 void hostap_handle_sta_tx_exc(local_info_t *local, struct sk_buff *skb)
2870 {
2871         struct sta_info *sta;
2872         struct ieee80211_hdr *hdr;
2873         struct hostap_skb_tx_data *meta;
2874
2875         hdr = (struct ieee80211_hdr *) skb->data;
2876         meta = (struct hostap_skb_tx_data *) skb->cb;
2877
2878         spin_lock(&local->ap->sta_table_lock);
2879         sta = ap_get_sta(local->ap, hdr->addr1);
2880         if (!sta) {
2881                 spin_unlock(&local->ap->sta_table_lock);
2882                 PDEBUG(DEBUG_AP, "%s: Could not find STA %pM"
2883                        " for this TX error (@%lu)\n",
2884                        local->dev->name, hdr->addr1, jiffies);
2885                 return;
2886         }
2887
2888         sta->tx_since_last_failure = 0;
2889         sta->tx_consecutive_exc++;
2890
2891         if (sta->tx_consecutive_exc >= WLAN_RATE_DECREASE_THRESHOLD &&
2892             sta->tx_rate_idx > 0 && meta->rate <= sta->tx_rate) {
2893                 /* use next lower rate */
2894                 int old, rate;
2895                 old = rate = sta->tx_rate_idx;
2896                 while (rate > 0) {
2897                         rate--;
2898                         if (ap_tx_rate_ok(rate, sta, local)) {
2899                                 sta->tx_rate_idx = rate;
2900                                 break;
2901                         }
2902                 }
2903                 if (old != sta->tx_rate_idx) {
2904                         switch (sta->tx_rate_idx) {
2905                         case 0: sta->tx_rate = 10; break;
2906                         case 1: sta->tx_rate = 20; break;
2907                         case 2: sta->tx_rate = 55; break;
2908                         case 3: sta->tx_rate = 110; break;
2909                         default: sta->tx_rate = 0; break;
2910                         }
2911                         PDEBUG(DEBUG_AP,
2912                                "%s: STA %pM TX rate lowered to %d\n",
2913                                local->dev->name, sta->addr, sta->tx_rate);
2914                 }
2915                 sta->tx_consecutive_exc = 0;
2916         }
2917         spin_unlock(&local->ap->sta_table_lock);
2918 }
2919
2920
2921 static void hostap_update_sta_ps2(local_info_t *local, struct sta_info *sta,
2922                                   int pwrmgt, int type, int stype)
2923 {
2924         if (pwrmgt && !(sta->flags & WLAN_STA_PS)) {
2925                 sta->flags |= WLAN_STA_PS;
2926                 PDEBUG(DEBUG_PS2, "STA %pM changed to use PS "
2927                        "mode (type=0x%02X, stype=0x%02X)\n",
2928                        sta->addr, type >> 2, stype >> 4);
2929         } else if (!pwrmgt && (sta->flags & WLAN_STA_PS)) {
2930                 sta->flags &= ~WLAN_STA_PS;
2931                 PDEBUG(DEBUG_PS2, "STA %pM changed to not use "
2932                        "PS mode (type=0x%02X, stype=0x%02X)\n",
2933                        sta->addr, type >> 2, stype >> 4);
2934                 if (type != IEEE80211_FTYPE_CTL ||
2935                     stype != IEEE80211_STYPE_PSPOLL)
2936                         schedule_packet_send(local, sta);
2937         }
2938 }
2939
2940
2941 /* Called only as a tasklet (software IRQ). Called for each RX frame to update
2942  * STA power saving state. pwrmgt is a flag from 802.11 frame_control field. */
2943 int hostap_update_sta_ps(local_info_t *local, struct ieee80211_hdr *hdr)
2944 {
2945         struct sta_info *sta;
2946         u16 fc;
2947
2948         spin_lock(&local->ap->sta_table_lock);
2949         sta = ap_get_sta(local->ap, hdr->addr2);
2950         if (sta)
2951                 atomic_inc(&sta->users);
2952         spin_unlock(&local->ap->sta_table_lock);
2953
2954         if (!sta)
2955                 return -1;
2956
2957         fc = le16_to_cpu(hdr->frame_control);
2958         hostap_update_sta_ps2(local, sta, fc & IEEE80211_FCTL_PM,
2959                               fc & IEEE80211_FCTL_FTYPE,
2960                               fc & IEEE80211_FCTL_STYPE);
2961
2962         atomic_dec(&sta->users);
2963         return 0;
2964 }
2965
2966
2967 /* Called only as a tasklet (software IRQ). Called for each RX frame after
2968  * getting RX header and payload from hardware. */
2969 ap_rx_ret hostap_handle_sta_rx(local_info_t *local, struct net_device *dev,
2970                                struct sk_buff *skb,
2971                                struct hostap_80211_rx_status *rx_stats,
2972                                int wds)
2973 {
2974         int ret;
2975         struct sta_info *sta;
2976         u16 fc, type, stype;
2977         struct ieee80211_hdr *hdr;
2978
2979         if (local->ap == NULL)
2980                 return AP_RX_CONTINUE;
2981
2982         hdr = (struct ieee80211_hdr *) skb->data;
2983
2984         fc = le16_to_cpu(hdr->frame_control);
2985         type = fc & IEEE80211_FCTL_FTYPE;
2986         stype = fc & IEEE80211_FCTL_STYPE;
2987
2988         spin_lock(&local->ap->sta_table_lock);
2989         sta = ap_get_sta(local->ap, hdr->addr2);
2990         if (sta)
2991                 atomic_inc(&sta->users);
2992         spin_unlock(&local->ap->sta_table_lock);
2993
2994         if (sta && !(sta->flags & WLAN_STA_AUTHORIZED))
2995                 ret = AP_RX_CONTINUE_NOT_AUTHORIZED;
2996         else
2997                 ret = AP_RX_CONTINUE;
2998
2999
3000         if (fc & IEEE80211_FCTL_TODS) {
3001                 if (!wds && (sta == NULL || !(sta->flags & WLAN_STA_ASSOC))) {
3002                         if (local->hostapd) {
3003                                 prism2_rx_80211(local->apdev, skb, rx_stats,
3004                                                 PRISM2_RX_NON_ASSOC);
3005 #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
3006                         } else {
3007                                 printk(KERN_DEBUG "%s: dropped received packet"
3008                                        " from non-associated STA %pM"
3009                                        " (type=0x%02x, subtype=0x%02x)\n",
3010                                        dev->name, hdr->addr2,
3011                                        type >> 2, stype >> 4);
3012                                 hostap_rx(dev, skb, rx_stats);
3013 #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
3014                         }
3015                         ret = AP_RX_EXIT;
3016                         goto out;
3017                 }
3018         } else if (fc & IEEE80211_FCTL_FROMDS) {
3019                 if (!wds) {
3020                         /* FromDS frame - not for us; probably
3021                          * broadcast/multicast in another BSS - drop */
3022                         if (ether_addr_equal(hdr->addr1, dev->dev_addr)) {
3023                                 printk(KERN_DEBUG "Odd.. FromDS packet "
3024                                        "received with own BSSID\n");
3025                                 hostap_dump_rx_80211(dev->name, skb, rx_stats);
3026                         }
3027                         ret = AP_RX_DROP;
3028                         goto out;
3029                 }
3030         } else if (stype == IEEE80211_STYPE_NULLFUNC && sta == NULL &&
3031                    ether_addr_equal(hdr->addr1, dev->dev_addr)) {
3032
3033                 if (local->hostapd) {
3034                         prism2_rx_80211(local->apdev, skb, rx_stats,
3035                                         PRISM2_RX_NON_ASSOC);
3036 #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
3037                 } else {
3038                         /* At least Lucent f/w seems to send data::nullfunc
3039                          * frames with no ToDS flag when the current AP returns
3040                          * after being unavailable for some time. Speed up
3041                          * re-association by informing the station about it not
3042                          * being associated. */
3043                         printk(KERN_DEBUG "%s: rejected received nullfunc frame"
3044                                " without ToDS from not associated STA %pM\n",
3045                                dev->name, hdr->addr2);
3046                         hostap_rx(dev, skb, rx_stats);
3047 #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
3048                 }
3049                 ret = AP_RX_EXIT;
3050                 goto out;
3051         } else if (stype == IEEE80211_STYPE_NULLFUNC) {
3052                 /* At least Lucent cards seem to send periodic nullfunc
3053                  * frames with ToDS. Let these through to update SQ
3054                  * stats and PS state. Nullfunc frames do not contain
3055                  * any data and they will be dropped below. */
3056         } else {
3057                 /* If BSSID (Addr3) is foreign, this frame is a normal
3058                  * broadcast frame from an IBSS network. Drop it silently.
3059                  * If BSSID is own, report the dropping of this frame. */
3060                 if (ether_addr_equal(hdr->addr3, dev->dev_addr)) {
3061                         printk(KERN_DEBUG "%s: dropped received packet from %pM"
3062                                " with no ToDS flag "
3063                                "(type=0x%02x, subtype=0x%02x)\n", dev->name,
3064                                hdr->addr2, type >> 2, stype >> 4);
3065                         hostap_dump_rx_80211(dev->name, skb, rx_stats);
3066                 }
3067                 ret = AP_RX_DROP;
3068                 goto out;
3069         }
3070
3071         if (sta) {
3072                 hostap_update_sta_ps2(local, sta, fc & IEEE80211_FCTL_PM,
3073                                       type, stype);
3074
3075                 sta->rx_packets++;
3076                 sta->rx_bytes += skb->len;
3077                 sta->last_rx = jiffies;
3078         }
3079
3080         if (local->ap->nullfunc_ack && stype == IEEE80211_STYPE_NULLFUNC &&
3081             fc & IEEE80211_FCTL_TODS) {
3082                 if (local->hostapd) {
3083                         prism2_rx_80211(local->apdev, skb, rx_stats,
3084                                         PRISM2_RX_NULLFUNC_ACK);
3085 #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
3086                 } else {
3087                         /* some STA f/w's seem to require control::ACK frame
3088                          * for data::nullfunc, but Prism2 f/w 0.8.0 (at least
3089                          * from Compaq) does not send this.. Try to generate
3090                          * ACK for these frames from the host driver to make
3091                          * power saving work with, e.g., Lucent WaveLAN f/w */
3092                         hostap_rx(dev, skb, rx_stats);
3093 #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
3094                 }
3095                 ret = AP_RX_EXIT;
3096                 goto out;
3097         }
3098
3099  out:
3100         if (sta)
3101                 atomic_dec(&sta->users);
3102
3103         return ret;
3104 }
3105
3106
3107 /* Called only as a tasklet (software IRQ) */
3108 int hostap_handle_sta_crypto(local_info_t *local,
3109                              struct ieee80211_hdr *hdr,
3110                              struct lib80211_crypt_data **crypt,
3111                              void **sta_ptr)
3112 {
3113         struct sta_info *sta;
3114
3115         spin_lock(&local->ap->sta_table_lock);
3116         sta = ap_get_sta(local->ap, hdr->addr2);
3117         if (sta)
3118                 atomic_inc(&sta->users);
3119         spin_unlock(&local->ap->sta_table_lock);
3120
3121         if (!sta)
3122                 return -1;
3123
3124         if (sta->crypt) {
3125                 *crypt = sta->crypt;
3126                 *sta_ptr = sta;
3127                 /* hostap_handle_sta_release() will be called to release STA
3128                  * info */
3129         } else
3130                 atomic_dec(&sta->users);
3131
3132         return 0;
3133 }
3134
3135
3136 /* Called only as a tasklet (software IRQ) */
3137 int hostap_is_sta_assoc(struct ap_data *ap, u8 *sta_addr)
3138 {
3139         struct sta_info *sta;
3140         int ret = 0;
3141
3142         spin_lock(&ap->sta_table_lock);
3143         sta = ap_get_sta(ap, sta_addr);
3144         if (sta != NULL && (sta->flags & WLAN_STA_ASSOC) && !sta->ap)
3145                 ret = 1;
3146         spin_unlock(&ap->sta_table_lock);
3147
3148         return ret;
3149 }
3150
3151
3152 /* Called only as a tasklet (software IRQ) */
3153 int hostap_is_sta_authorized(struct ap_data *ap, u8 *sta_addr)
3154 {
3155         struct sta_info *sta;
3156         int ret = 0;
3157
3158         spin_lock(&ap->sta_table_lock);
3159         sta = ap_get_sta(ap, sta_addr);
3160         if (sta != NULL && (sta->flags & WLAN_STA_ASSOC) && !sta->ap &&
3161             ((sta->flags & WLAN_STA_AUTHORIZED) ||
3162              ap->local->ieee_802_1x == 0))
3163                 ret = 1;
3164         spin_unlock(&ap->sta_table_lock);
3165
3166         return ret;
3167 }
3168
3169
3170 /* Called only as a tasklet (software IRQ) */
3171 int hostap_add_sta(struct ap_data *ap, u8 *sta_addr)
3172 {
3173         struct sta_info *sta;
3174         int ret = 1;
3175
3176         if (!ap)
3177                 return -1;
3178
3179         spin_lock(&ap->sta_table_lock);
3180         sta = ap_get_sta(ap, sta_addr);
3181         if (sta)
3182                 ret = 0;
3183         spin_unlock(&ap->sta_table_lock);
3184
3185         if (ret == 1) {
3186                 sta = ap_add_sta(ap, sta_addr);
3187                 if (!sta)
3188                         return -1;
3189                 sta->flags = WLAN_STA_AUTH | WLAN_STA_ASSOC;
3190                 sta->ap = 1;
3191                 memset(sta->supported_rates, 0, sizeof(sta->supported_rates));
3192                 /* No way of knowing which rates are supported since we did not
3193                  * get supported rates element from beacon/assoc req. Assume
3194                  * that remote end supports all 802.11b rates. */
3195                 sta->supported_rates[0] = 0x82;
3196                 sta->supported_rates[1] = 0x84;
3197                 sta->supported_rates[2] = 0x0b;
3198                 sta->supported_rates[3] = 0x16;
3199                 sta->tx_supp_rates = WLAN_RATE_1M | WLAN_RATE_2M |
3200                         WLAN_RATE_5M5 | WLAN_RATE_11M;
3201                 sta->tx_rate = 110;
3202                 sta->tx_max_rate = sta->tx_rate_idx = 3;
3203         }
3204
3205         return ret;
3206 }
3207
3208
3209 /* Called only as a tasklet (software IRQ) */
3210 int hostap_update_rx_stats(struct ap_data *ap,
3211                            struct ieee80211_hdr *hdr,
3212                            struct hostap_80211_rx_status *rx_stats)
3213 {
3214         struct sta_info *sta;
3215
3216         if (!ap)
3217                 return -1;
3218
3219         spin_lock(&ap->sta_table_lock);
3220         sta = ap_get_sta(ap, hdr->addr2);
3221         if (sta) {
3222                 sta->last_rx_silence = rx_stats->noise;
3223                 sta->last_rx_signal = rx_stats->signal;
3224                 sta->last_rx_rate = rx_stats->rate;
3225                 sta->last_rx_updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
3226                 if (rx_stats->rate == 10)
3227                         sta->rx_count[0]++;
3228                 else if (rx_stats->rate == 20)
3229                         sta->rx_count[1]++;
3230                 else if (rx_stats->rate == 55)
3231                         sta->rx_count[2]++;
3232                 else if (rx_stats->rate == 110)
3233                         sta->rx_count[3]++;
3234         }
3235         spin_unlock(&ap->sta_table_lock);
3236
3237         return sta ? 0 : -1;
3238 }
3239
3240
3241 void hostap_update_rates(local_info_t *local)
3242 {
3243         struct sta_info *sta;
3244         struct ap_data *ap = local->ap;
3245
3246         if (!ap)
3247                 return;
3248
3249         spin_lock_bh(&ap->sta_table_lock);
3250         list_for_each_entry(sta, &ap->sta_list, list) {
3251                 prism2_check_tx_rates(sta);
3252         }
3253         spin_unlock_bh(&ap->sta_table_lock);
3254 }
3255
3256
3257 void * ap_crypt_get_ptrs(struct ap_data *ap, u8 *addr, int permanent,
3258                          struct lib80211_crypt_data ***crypt)
3259 {
3260         struct sta_info *sta;
3261
3262         spin_lock_bh(&ap->sta_table_lock);
3263         sta = ap_get_sta(ap, addr);
3264         if (sta)
3265                 atomic_inc(&sta->users);
3266         spin_unlock_bh(&ap->sta_table_lock);
3267
3268         if (!sta && permanent)
3269                 sta = ap_add_sta(ap, addr);
3270
3271         if (!sta)
3272                 return NULL;
3273
3274         if (permanent)
3275                 sta->flags |= WLAN_STA_PERM;
3276
3277         *crypt = &sta->crypt;
3278
3279         return sta;
3280 }
3281
3282
3283 void hostap_add_wds_links(local_info_t *local)
3284 {
3285         struct ap_data *ap = local->ap;
3286         struct sta_info *sta;
3287
3288         spin_lock_bh(&ap->sta_table_lock);
3289         list_for_each_entry(sta, &ap->sta_list, list) {
3290                 if (sta->ap)
3291                         hostap_wds_link_oper(local, sta->addr, WDS_ADD);
3292         }
3293         spin_unlock_bh(&ap->sta_table_lock);
3294
3295         schedule_work(&local->ap->wds_oper_queue);
3296 }
3297
3298
3299 void hostap_wds_link_oper(local_info_t *local, u8 *addr, wds_oper_type type)
3300 {
3301         struct wds_oper_data *entry;
3302
3303         entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
3304         if (!entry)
3305                 return;
3306         memcpy(entry->addr, addr, ETH_ALEN);
3307         entry->type = type;
3308         spin_lock_bh(&local->lock);
3309         entry->next = local->ap->wds_oper_entries;
3310         local->ap->wds_oper_entries = entry;
3311         spin_unlock_bh(&local->lock);
3312
3313         schedule_work(&local->ap->wds_oper_queue);
3314 }
3315
3316
3317 EXPORT_SYMBOL(hostap_init_data);
3318 EXPORT_SYMBOL(hostap_init_ap_proc);
3319 EXPORT_SYMBOL(hostap_free_data);
3320 EXPORT_SYMBOL(hostap_check_sta_fw_version);
3321 EXPORT_SYMBOL(hostap_handle_sta_tx_exc);
3322 #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
3323 #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */