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