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