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