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