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