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