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