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