blob: 0903db786d5f94d565e67ed82d7a42216eb41cf9 [file] [log] [blame]
Jouni Malinenff1d2762005-05-12 22:54:16 -04001/*
2 * Intersil Prism2 driver with Host AP (software access point) support
3 * Copyright (c) 2001-2002, SSH Communications Security Corp and Jouni Malinen
Jouni Malinen85d32e72007-03-24 17:15:30 -07004 * <j@w1.fi>
5 * Copyright (c) 2002-2005, Jouni Malinen <j@w1.fi>
Jouni Malinenff1d2762005-05-12 22:54:16 -04006 *
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
Adrian Bunk5fad5a22006-01-14 03:09:34 +010019#include <linux/proc_fs.h>
20#include <linux/delay.h>
21#include <linux/random.h>
22
23#include "hostap_wlan.h"
24#include "hostap.h"
25#include "hostap_ap.h"
26
Jouni Malinenff1d2762005-05-12 22:54:16 -040027static int other_ap_policy[MAX_PARM_DEVICES] = { AP_OTHER_AP_SKIP_ALL,
28 DEF_INTS };
29module_param_array(other_ap_policy, int, NULL, 0444);
30MODULE_PARM_DESC(other_ap_policy, "Other AP beacon monitoring policy (0-3)");
31
32static int ap_max_inactivity[MAX_PARM_DEVICES] = { AP_MAX_INACTIVITY_SEC,
33 DEF_INTS };
34module_param_array(ap_max_inactivity, int, NULL, 0444);
35MODULE_PARM_DESC(ap_max_inactivity, "AP timeout (in seconds) for station "
36 "inactivity");
37
38static int ap_bridge_packets[MAX_PARM_DEVICES] = { 1, DEF_INTS };
39module_param_array(ap_bridge_packets, int, NULL, 0444);
40MODULE_PARM_DESC(ap_bridge_packets, "Bridge packets directly between "
41 "stations");
42
43static int autom_ap_wds[MAX_PARM_DEVICES] = { 0, DEF_INTS };
44module_param_array(autom_ap_wds, int, NULL, 0444);
45MODULE_PARM_DESC(autom_ap_wds, "Add WDS connections to other APs "
46 "automatically");
47
48
49static struct sta_info* ap_get_sta(struct ap_data *ap, u8 *sta);
50static void hostap_event_expired_sta(struct net_device *dev,
51 struct sta_info *sta);
David Howellsc4028952006-11-22 14:57:56 +000052static void handle_add_proc_queue(struct work_struct *work);
Jouni Malinenff1d2762005-05-12 22:54:16 -040053
54#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
David Howellsc4028952006-11-22 14:57:56 +000055static void handle_wds_oper_queue(struct work_struct *work);
Jouni Malinenff1d2762005-05-12 22:54:16 -040056static void prism2_send_mgmt(struct net_device *dev,
Jouni Malinen4339d322005-08-14 19:08:44 -070057 u16 type_subtype, char *body,
Jouni Malinenff1d2762005-05-12 22:54:16 -040058 int body_len, u8 *addr, u16 tx_cb_idx);
59#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
60
61
62#ifndef PRISM2_NO_PROCFS_DEBUG
63static int ap_debug_proc_read(char *page, char **start, off_t off,
64 int count, int *eof, void *data)
65{
66 char *p = page;
67 struct ap_data *ap = (struct ap_data *) data;
68
69 if (off != 0) {
70 *eof = 1;
71 return 0;
72 }
73
74 p += sprintf(p, "BridgedUnicastFrames=%u\n", ap->bridged_unicast);
75 p += sprintf(p, "BridgedMulticastFrames=%u\n", ap->bridged_multicast);
76 p += sprintf(p, "max_inactivity=%u\n", ap->max_inactivity / HZ);
77 p += sprintf(p, "bridge_packets=%u\n", ap->bridge_packets);
78 p += sprintf(p, "nullfunc_ack=%u\n", ap->nullfunc_ack);
79 p += sprintf(p, "autom_ap_wds=%u\n", ap->autom_ap_wds);
80 p += sprintf(p, "auth_algs=%u\n", ap->local->auth_algs);
81 p += sprintf(p, "tx_drop_nonassoc=%u\n", ap->tx_drop_nonassoc);
82
83 return (p - page);
84}
85#endif /* PRISM2_NO_PROCFS_DEBUG */
86
87
88static void ap_sta_hash_add(struct ap_data *ap, struct sta_info *sta)
89{
90 sta->hnext = ap->sta_hash[STA_HASH(sta->addr)];
91 ap->sta_hash[STA_HASH(sta->addr)] = sta;
92}
93
94static void ap_sta_hash_del(struct ap_data *ap, struct sta_info *sta)
95{
96 struct sta_info *s;
97
98 s = ap->sta_hash[STA_HASH(sta->addr)];
99 if (s == NULL) return;
100 if (memcmp(s->addr, sta->addr, ETH_ALEN) == 0) {
101 ap->sta_hash[STA_HASH(sta->addr)] = s->hnext;
102 return;
103 }
104
105 while (s->hnext != NULL && memcmp(s->hnext->addr, sta->addr, ETH_ALEN)
106 != 0)
107 s = s->hnext;
108 if (s->hnext != NULL)
109 s->hnext = s->hnext->hnext;
110 else
Johannes Berge1749612008-10-27 15:59:26 -0700111 printk("AP: could not remove STA %pM from hash table\n",
112 sta->addr);
Jouni Malinenff1d2762005-05-12 22:54:16 -0400113}
114
115static void ap_free_sta(struct ap_data *ap, struct sta_info *sta)
116{
117 if (sta->ap && sta->local)
118 hostap_event_expired_sta(sta->local->dev, sta);
119
120 if (ap->proc != NULL) {
121 char name[20];
Johannes Berge1749612008-10-27 15:59:26 -0700122 sprintf(name, "%pM", sta->addr);
Jouni Malinenff1d2762005-05-12 22:54:16 -0400123 remove_proc_entry(name, ap->proc);
124 }
125
126 if (sta->crypt) {
127 sta->crypt->ops->deinit(sta->crypt->priv);
128 kfree(sta->crypt);
129 sta->crypt = NULL;
130 }
131
132 skb_queue_purge(&sta->tx_buf);
133
134 ap->num_sta--;
135#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
136 if (sta->aid > 0)
137 ap->sta_aid[sta->aid - 1] = NULL;
138
139 if (!sta->ap && sta->u.sta.challenge)
140 kfree(sta->u.sta.challenge);
141 del_timer(&sta->timer);
142#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
143
144 kfree(sta);
145}
146
147
148static void hostap_set_tim(local_info_t *local, int aid, int set)
149{
150 if (local->func->set_tim)
151 local->func->set_tim(local->dev, aid, set);
152}
153
154
155static void hostap_event_new_sta(struct net_device *dev, struct sta_info *sta)
156{
157 union iwreq_data wrqu;
158 memset(&wrqu, 0, sizeof(wrqu));
159 memcpy(wrqu.addr.sa_data, sta->addr, ETH_ALEN);
160 wrqu.addr.sa_family = ARPHRD_ETHER;
161 wireless_send_event(dev, IWEVREGISTERED, &wrqu, NULL);
162}
163
164
165static void hostap_event_expired_sta(struct net_device *dev,
166 struct sta_info *sta)
167{
168 union iwreq_data wrqu;
169 memset(&wrqu, 0, sizeof(wrqu));
170 memcpy(wrqu.addr.sa_data, sta->addr, ETH_ALEN);
171 wrqu.addr.sa_family = ARPHRD_ETHER;
172 wireless_send_event(dev, IWEVEXPIRED, &wrqu, NULL);
173}
174
175
176#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
177
178static void ap_handle_timer(unsigned long data)
179{
180 struct sta_info *sta = (struct sta_info *) data;
181 local_info_t *local;
182 struct ap_data *ap;
183 unsigned long next_time = 0;
184 int was_assoc;
185
186 if (sta == NULL || sta->local == NULL || sta->local->ap == NULL) {
187 PDEBUG(DEBUG_AP, "ap_handle_timer() called with NULL data\n");
188 return;
189 }
190
191 local = sta->local;
192 ap = local->ap;
193 was_assoc = sta->flags & WLAN_STA_ASSOC;
194
195 if (atomic_read(&sta->users) != 0)
196 next_time = jiffies + HZ;
197 else if ((sta->flags & WLAN_STA_PERM) && !(sta->flags & WLAN_STA_AUTH))
198 next_time = jiffies + ap->max_inactivity;
199
200 if (time_before(jiffies, sta->last_rx + ap->max_inactivity)) {
201 /* station activity detected; reset timeout state */
202 sta->timeout_next = STA_NULLFUNC;
203 next_time = sta->last_rx + ap->max_inactivity;
204 } else if (sta->timeout_next == STA_DISASSOC &&
205 !(sta->flags & WLAN_STA_PENDING_POLL)) {
206 /* STA ACKed data nullfunc frame poll */
207 sta->timeout_next = STA_NULLFUNC;
208 next_time = jiffies + ap->max_inactivity;
209 }
210
211 if (next_time) {
212 sta->timer.expires = next_time;
213 add_timer(&sta->timer);
214 return;
215 }
216
217 if (sta->ap)
218 sta->timeout_next = STA_DEAUTH;
219
220 if (sta->timeout_next == STA_DEAUTH && !(sta->flags & WLAN_STA_PERM)) {
221 spin_lock(&ap->sta_table_lock);
222 ap_sta_hash_del(ap, sta);
223 list_del(&sta->list);
224 spin_unlock(&ap->sta_table_lock);
225 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
226 } else if (sta->timeout_next == STA_DISASSOC)
227 sta->flags &= ~WLAN_STA_ASSOC;
228
229 if (was_assoc && !(sta->flags & WLAN_STA_ASSOC) && !sta->ap)
230 hostap_event_expired_sta(local->dev, sta);
231
232 if (sta->timeout_next == STA_DEAUTH && sta->aid > 0 &&
233 !skb_queue_empty(&sta->tx_buf)) {
234 hostap_set_tim(local, sta->aid, 0);
235 sta->flags &= ~WLAN_STA_TIM;
236 }
237
238 if (sta->ap) {
239 if (ap->autom_ap_wds) {
240 PDEBUG(DEBUG_AP, "%s: removing automatic WDS "
Johannes Berge1749612008-10-27 15:59:26 -0700241 "connection to AP %pM\n",
242 local->dev->name, sta->addr);
Jouni Malinenff1d2762005-05-12 22:54:16 -0400243 hostap_wds_link_oper(local, sta->addr, WDS_DEL);
244 }
245 } else if (sta->timeout_next == STA_NULLFUNC) {
246 /* send data frame to poll STA and check whether this frame
247 * is ACKed */
Jouni Malinen4339d322005-08-14 19:08:44 -0700248 /* FIX: IEEE80211_STYPE_NULLFUNC would be more appropriate, but
Jouni Malinenff1d2762005-05-12 22:54:16 -0400249 * it is apparently not retried so TX Exc events are not
250 * received for it */
251 sta->flags |= WLAN_STA_PENDING_POLL;
Jouni Malinen4339d322005-08-14 19:08:44 -0700252 prism2_send_mgmt(local->dev, IEEE80211_FTYPE_DATA |
253 IEEE80211_STYPE_DATA, NULL, 0,
Jouni Malinenff1d2762005-05-12 22:54:16 -0400254 sta->addr, ap->tx_callback_poll);
255 } else {
256 int deauth = sta->timeout_next == STA_DEAUTH;
Al Viro8a9faf32007-12-21 03:30:16 -0500257 __le16 resp;
Johannes Berge1749612008-10-27 15:59:26 -0700258 PDEBUG(DEBUG_AP, "%s: sending %s info to STA %pM"
Jouni Malinenff1d2762005-05-12 22:54:16 -0400259 "(last=%lu, jiffies=%lu)\n",
260 local->dev->name,
261 deauth ? "deauthentication" : "disassociation",
Johannes Berge1749612008-10-27 15:59:26 -0700262 sta->addr, sta->last_rx, jiffies);
Jouni Malinenff1d2762005-05-12 22:54:16 -0400263
264 resp = cpu_to_le16(deauth ? WLAN_REASON_PREV_AUTH_NOT_VALID :
265 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
Jouni Malinen4339d322005-08-14 19:08:44 -0700266 prism2_send_mgmt(local->dev, IEEE80211_FTYPE_MGMT |
267 (deauth ? IEEE80211_STYPE_DEAUTH :
268 IEEE80211_STYPE_DISASSOC),
Jouni Malinenff1d2762005-05-12 22:54:16 -0400269 (char *) &resp, 2, sta->addr, 0);
270 }
271
272 if (sta->timeout_next == STA_DEAUTH) {
273 if (sta->flags & WLAN_STA_PERM) {
Johannes Berge1749612008-10-27 15:59:26 -0700274 PDEBUG(DEBUG_AP, "%s: STA %pM"
Joe Perches0795af52007-10-03 17:59:30 -0700275 " would have been removed, "
276 "but it has 'perm' flag\n",
Johannes Berge1749612008-10-27 15:59:26 -0700277 local->dev->name, sta->addr);
Jouni Malinenff1d2762005-05-12 22:54:16 -0400278 } else
279 ap_free_sta(ap, sta);
280 return;
281 }
282
283 if (sta->timeout_next == STA_NULLFUNC) {
284 sta->timeout_next = STA_DISASSOC;
285 sta->timer.expires = jiffies + AP_DISASSOC_DELAY;
286 } else {
287 sta->timeout_next = STA_DEAUTH;
288 sta->timer.expires = jiffies + AP_DEAUTH_DELAY;
289 }
290
291 add_timer(&sta->timer);
292}
293
294
295void hostap_deauth_all_stas(struct net_device *dev, struct ap_data *ap,
296 int resend)
297{
298 u8 addr[ETH_ALEN];
Al Viro8a9faf32007-12-21 03:30:16 -0500299 __le16 resp;
Jouni Malinenff1d2762005-05-12 22:54:16 -0400300 int i;
301
302 PDEBUG(DEBUG_AP, "%s: Deauthenticate all stations\n", dev->name);
303 memset(addr, 0xff, ETH_ALEN);
304
Al Viro8a9faf32007-12-21 03:30:16 -0500305 resp = cpu_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
Jouni Malinenff1d2762005-05-12 22:54:16 -0400306
307 /* deauth message sent; try to resend it few times; the message is
308 * broadcast, so it may be delayed until next DTIM; there is not much
309 * else we can do at this point since the driver is going to be shut
310 * down */
311 for (i = 0; i < 5; i++) {
Jouni Malinen4339d322005-08-14 19:08:44 -0700312 prism2_send_mgmt(dev, IEEE80211_FTYPE_MGMT |
313 IEEE80211_STYPE_DEAUTH,
Jouni Malinenff1d2762005-05-12 22:54:16 -0400314 (char *) &resp, 2, addr, 0);
315
316 if (!resend || ap->num_sta <= 0)
317 return;
318
319 mdelay(50);
320 }
321}
322
323
324static int ap_control_proc_read(char *page, char **start, off_t off,
325 int count, int *eof, void *data)
326{
327 char *p = page;
328 struct ap_data *ap = (struct ap_data *) data;
329 char *policy_txt;
Jouni Malinenff1d2762005-05-12 22:54:16 -0400330 struct mac_entry *entry;
331
332 if (off != 0) {
333 *eof = 1;
334 return 0;
335 }
336
337 switch (ap->mac_restrictions.policy) {
338 case MAC_POLICY_OPEN:
339 policy_txt = "open";
340 break;
341 case MAC_POLICY_ALLOW:
342 policy_txt = "allow";
343 break;
344 case MAC_POLICY_DENY:
345 policy_txt = "deny";
346 break;
347 default:
348 policy_txt = "unknown";
349 break;
350 };
351 p += sprintf(p, "MAC policy: %s\n", policy_txt);
352 p += sprintf(p, "MAC entries: %u\n", ap->mac_restrictions.entries);
353 p += sprintf(p, "MAC list:\n");
354 spin_lock_bh(&ap->mac_restrictions.lock);
Matthias Kaehlckec1505732007-05-28 09:38:48 -0700355 list_for_each_entry(entry, &ap->mac_restrictions.mac_list, list) {
Jouni Malinenff1d2762005-05-12 22:54:16 -0400356 if (p - page > PAGE_SIZE - 80) {
357 p += sprintf(p, "All entries did not fit one page.\n");
358 break;
359 }
360
Johannes Berge1749612008-10-27 15:59:26 -0700361 p += sprintf(p, "%pM\n", entry->addr);
Jouni Malinenff1d2762005-05-12 22:54:16 -0400362 }
363 spin_unlock_bh(&ap->mac_restrictions.lock);
364
365 return (p - page);
366}
367
368
Adrian Bunk5fad5a22006-01-14 03:09:34 +0100369int ap_control_add_mac(struct mac_restrictions *mac_restrictions, u8 *mac)
Jouni Malinenff1d2762005-05-12 22:54:16 -0400370{
371 struct mac_entry *entry;
372
373 entry = kmalloc(sizeof(struct mac_entry), GFP_KERNEL);
374 if (entry == NULL)
375 return -1;
376
377 memcpy(entry->addr, mac, ETH_ALEN);
378
379 spin_lock_bh(&mac_restrictions->lock);
380 list_add_tail(&entry->list, &mac_restrictions->mac_list);
381 mac_restrictions->entries++;
382 spin_unlock_bh(&mac_restrictions->lock);
383
384 return 0;
385}
386
387
Adrian Bunk5fad5a22006-01-14 03:09:34 +0100388int ap_control_del_mac(struct mac_restrictions *mac_restrictions, u8 *mac)
Jouni Malinenff1d2762005-05-12 22:54:16 -0400389{
390 struct list_head *ptr;
391 struct mac_entry *entry;
392
393 spin_lock_bh(&mac_restrictions->lock);
394 for (ptr = mac_restrictions->mac_list.next;
395 ptr != &mac_restrictions->mac_list; ptr = ptr->next) {
396 entry = list_entry(ptr, struct mac_entry, list);
397
398 if (memcmp(entry->addr, mac, ETH_ALEN) == 0) {
399 list_del(ptr);
400 kfree(entry);
401 mac_restrictions->entries--;
402 spin_unlock_bh(&mac_restrictions->lock);
403 return 0;
404 }
405 }
406 spin_unlock_bh(&mac_restrictions->lock);
407 return -1;
408}
409
410
411static int ap_control_mac_deny(struct mac_restrictions *mac_restrictions,
412 u8 *mac)
413{
Jouni Malinenff1d2762005-05-12 22:54:16 -0400414 struct mac_entry *entry;
415 int found = 0;
416
417 if (mac_restrictions->policy == MAC_POLICY_OPEN)
418 return 0;
419
420 spin_lock_bh(&mac_restrictions->lock);
Matthias Kaehlckec1505732007-05-28 09:38:48 -0700421 list_for_each_entry(entry, &mac_restrictions->mac_list, list) {
Jouni Malinenff1d2762005-05-12 22:54:16 -0400422 if (memcmp(entry->addr, mac, ETH_ALEN) == 0) {
423 found = 1;
424 break;
425 }
426 }
427 spin_unlock_bh(&mac_restrictions->lock);
428
429 if (mac_restrictions->policy == MAC_POLICY_ALLOW)
430 return !found;
431 else
432 return found;
433}
434
435
Adrian Bunk5fad5a22006-01-14 03:09:34 +0100436void ap_control_flush_macs(struct mac_restrictions *mac_restrictions)
Jouni Malinenff1d2762005-05-12 22:54:16 -0400437{
438 struct list_head *ptr, *n;
439 struct mac_entry *entry;
440
441 if (mac_restrictions->entries == 0)
442 return;
443
444 spin_lock_bh(&mac_restrictions->lock);
445 for (ptr = mac_restrictions->mac_list.next, n = ptr->next;
446 ptr != &mac_restrictions->mac_list;
447 ptr = n, n = ptr->next) {
448 entry = list_entry(ptr, struct mac_entry, list);
449 list_del(ptr);
450 kfree(entry);
451 }
452 mac_restrictions->entries = 0;
453 spin_unlock_bh(&mac_restrictions->lock);
454}
455
456
Adrian Bunk5fad5a22006-01-14 03:09:34 +0100457int ap_control_kick_mac(struct ap_data *ap, struct net_device *dev, u8 *mac)
Jouni Malinenff1d2762005-05-12 22:54:16 -0400458{
459 struct sta_info *sta;
Al Viro8a9faf32007-12-21 03:30:16 -0500460 __le16 resp;
Jouni Malinenff1d2762005-05-12 22:54:16 -0400461
462 spin_lock_bh(&ap->sta_table_lock);
463 sta = ap_get_sta(ap, mac);
464 if (sta) {
465 ap_sta_hash_del(ap, sta);
466 list_del(&sta->list);
467 }
468 spin_unlock_bh(&ap->sta_table_lock);
469
470 if (!sta)
471 return -EINVAL;
472
473 resp = cpu_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
Jouni Malinen4339d322005-08-14 19:08:44 -0700474 prism2_send_mgmt(dev, IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_DEAUTH,
Jouni Malinenff1d2762005-05-12 22:54:16 -0400475 (char *) &resp, 2, sta->addr, 0);
476
477 if ((sta->flags & WLAN_STA_ASSOC) && !sta->ap)
478 hostap_event_expired_sta(dev, sta);
479
480 ap_free_sta(ap, sta);
481
482 return 0;
483}
484
485#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
486
487
Adrian Bunk5fad5a22006-01-14 03:09:34 +0100488void ap_control_kickall(struct ap_data *ap)
Jouni Malinenff1d2762005-05-12 22:54:16 -0400489{
490 struct list_head *ptr, *n;
491 struct sta_info *sta;
Jeff Garzik74fae822005-07-31 13:08:32 -0400492
Jouni Malinenff1d2762005-05-12 22:54:16 -0400493 spin_lock_bh(&ap->sta_table_lock);
494 for (ptr = ap->sta_list.next, n = ptr->next; ptr != &ap->sta_list;
495 ptr = n, n = ptr->next) {
496 sta = list_entry(ptr, struct sta_info, list);
497 ap_sta_hash_del(ap, sta);
498 list_del(&sta->list);
499 if ((sta->flags & WLAN_STA_ASSOC) && !sta->ap && sta->local)
500 hostap_event_expired_sta(sta->local->dev, sta);
501 ap_free_sta(ap, sta);
502 }
503 spin_unlock_bh(&ap->sta_table_lock);
504}
505
506
507#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
508
509#define PROC_LIMIT (PAGE_SIZE - 80)
510
511static int prism2_ap_proc_read(char *page, char **start, off_t off,
512 int count, int *eof, void *data)
513{
514 char *p = page;
515 struct ap_data *ap = (struct ap_data *) data;
Matthias Kaehlckec1505732007-05-28 09:38:48 -0700516 struct sta_info *sta;
Jouni Malinenff1d2762005-05-12 22:54:16 -0400517 int i;
518
519 if (off > PROC_LIMIT) {
520 *eof = 1;
521 return 0;
522 }
523
524 p += sprintf(p, "# BSSID CHAN SIGNAL NOISE RATE SSID FLAGS\n");
525 spin_lock_bh(&ap->sta_table_lock);
Matthias Kaehlckec1505732007-05-28 09:38:48 -0700526 list_for_each_entry(sta, &ap->sta_list, list) {
Jouni Malinenff1d2762005-05-12 22:54:16 -0400527 if (!sta->ap)
528 continue;
529
Johannes Berge1749612008-10-27 15:59:26 -0700530 p += sprintf(p, "%pM %d %d %d %d '",
531 sta->addr,
Jouni Malinenff1d2762005-05-12 22:54:16 -0400532 sta->u.ap.channel, sta->last_rx_signal,
533 sta->last_rx_silence, sta->last_rx_rate);
534 for (i = 0; i < sta->u.ap.ssid_len; i++)
535 p += sprintf(p, ((sta->u.ap.ssid[i] >= 32 &&
536 sta->u.ap.ssid[i] < 127) ?
537 "%c" : "<%02x>"),
538 sta->u.ap.ssid[i]);
539 p += sprintf(p, "'");
540 if (sta->capability & WLAN_CAPABILITY_ESS)
541 p += sprintf(p, " [ESS]");
542 if (sta->capability & WLAN_CAPABILITY_IBSS)
543 p += sprintf(p, " [IBSS]");
544 if (sta->capability & WLAN_CAPABILITY_PRIVACY)
545 p += sprintf(p, " [WEP]");
546 p += sprintf(p, "\n");
547
548 if ((p - page) > PROC_LIMIT) {
549 printk(KERN_DEBUG "hostap: ap proc did not fit\n");
550 break;
551 }
552 }
553 spin_unlock_bh(&ap->sta_table_lock);
554
555 if ((p - page) <= off) {
556 *eof = 1;
557 return 0;
558 }
559
560 *start = page + off;
561
562 return (p - page - off);
563}
564#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
565
566
567void hostap_check_sta_fw_version(struct ap_data *ap, int sta_fw_ver)
568{
569 if (!ap)
570 return;
571
572 if (sta_fw_ver == PRISM2_FW_VER(0,8,0)) {
573 PDEBUG(DEBUG_AP, "Using data::nullfunc ACK workaround - "
574 "firmware upgrade recommended\n");
575 ap->nullfunc_ack = 1;
576 } else
577 ap->nullfunc_ack = 0;
578
579 if (sta_fw_ver == PRISM2_FW_VER(1,4,2)) {
580 printk(KERN_WARNING "%s: Warning: secondary station firmware "
581 "version 1.4.2 does not seem to work in Host AP mode\n",
582 ap->local->dev->name);
583 }
584}
585
586
587/* Called only as a tasklet (software IRQ) */
588static void hostap_ap_tx_cb(struct sk_buff *skb, int ok, void *data)
589{
590 struct ap_data *ap = data;
591 u16 fc;
James Ketrenosd0416742005-09-21 12:23:49 -0500592 struct ieee80211_hdr_4addr *hdr;
Jouni Malinenff1d2762005-05-12 22:54:16 -0400593
594 if (!ap->local->hostapd || !ap->local->apdev) {
595 dev_kfree_skb(skb);
596 return;
597 }
598
James Ketrenosd0416742005-09-21 12:23:49 -0500599 hdr = (struct ieee80211_hdr_4addr *) skb->data;
Jouni Malinenc0f72ca2005-08-14 19:08:43 -0700600 fc = le16_to_cpu(hdr->frame_ctl);
Jouni Malinenff1d2762005-05-12 22:54:16 -0400601
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
Jouni Malinenb2f4a2e2005-08-14 21:00:01 -0700605 fc &= ~IEEE80211_FCTL_VERS;
Jouni Malinenff1d2762005-05-12 22:54:16 -0400606 fc |= ok ? BIT(1) : BIT(0);
Jouni Malinenc0f72ca2005-08-14 19:08:43 -0700607 hdr->frame_ctl = cpu_to_le16(fc);
Jouni Malinenff1d2762005-05-12 22:54:16 -0400608
609 skb->dev = ap->local->apdev;
610 skb_pull(skb, hostap_80211_get_hdrlen(fc));
611 skb->pkt_type = PACKET_OTHERHOST;
612 skb->protocol = __constant_htons(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;
James Ketrenosd0416742005-09-21 12:23:49 -0500624 struct ieee80211_hdr_4addr *hdr;
Al Viro8a9faf32007-12-21 03:30:16 -0500625 u16 fc, auth_alg, auth_transaction, status;
626 __le16 *pos;
Jouni Malinenff1d2762005-05-12 22:54:16 -0400627 struct sta_info *sta = NULL;
628 char *txt = NULL;
629
630 if (ap->local->hostapd) {
631 dev_kfree_skb(skb);
632 return;
633 }
634
James Ketrenosd0416742005-09-21 12:23:49 -0500635 hdr = (struct ieee80211_hdr_4addr *) skb->data;
Jouni Malinenc0f72ca2005-08-14 19:08:43 -0700636 fc = le16_to_cpu(hdr->frame_ctl);
Jouni Malinen4339d322005-08-14 19:08:44 -0700637 if (WLAN_FC_GET_TYPE(fc) != IEEE80211_FTYPE_MGMT ||
638 WLAN_FC_GET_STYPE(fc) != IEEE80211_STYPE_AUTH ||
Jouni Malinenff1d2762005-05-12 22:54:16 -0400639 skb->len < IEEE80211_MGMT_HDR_LEN + 6) {
640 printk(KERN_DEBUG "%s: hostap_ap_tx_cb_auth received invalid "
641 "frame\n", dev->name);
642 dev_kfree_skb(skb);
643 return;
644 }
645
Al Viro8a9faf32007-12-21 03:30:16 -0500646 pos = (__le16 *) (skb->data + IEEE80211_MGMT_HDR_LEN);
Jouni Malinenff1d2762005-05-12 22:54:16 -0400647 auth_alg = le16_to_cpu(*pos++);
648 auth_transaction = le16_to_cpu(*pos++);
649 status = le16_to_cpu(*pos++);
650
651 if (!ok) {
652 txt = "frame was not ACKed";
653 goto done;
654 }
655
656 spin_lock(&ap->sta_table_lock);
657 sta = ap_get_sta(ap, hdr->addr1);
658 if (sta)
659 atomic_inc(&sta->users);
660 spin_unlock(&ap->sta_table_lock);
661
662 if (!sta) {
663 txt = "STA not found";
664 goto done;
665 }
666
667 if (status == WLAN_STATUS_SUCCESS &&
668 ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
669 (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
670 txt = "STA authenticated";
671 sta->flags |= WLAN_STA_AUTH;
672 sta->last_auth = jiffies;
673 } else if (status != WLAN_STATUS_SUCCESS)
674 txt = "authentication failed";
675
676 done:
677 if (sta)
678 atomic_dec(&sta->users);
679 if (txt) {
Johannes Berge1749612008-10-27 15:59:26 -0700680 PDEBUG(DEBUG_AP, "%s: %pM auth_cb - alg=%d "
Joe Perches0795af52007-10-03 17:59:30 -0700681 "trans#=%d status=%d - %s\n",
Johannes Berge1749612008-10-27 15:59:26 -0700682 dev->name, hdr->addr1,
David S. Miller21f644f2008-04-08 16:50:44 -0700683 auth_alg, auth_transaction, status, txt);
Jouni Malinenff1d2762005-05-12 22:54:16 -0400684 }
685 dev_kfree_skb(skb);
686}
687
688
689/* Called only as a tasklet (software IRQ) */
690static void hostap_ap_tx_cb_assoc(struct sk_buff *skb, int ok, void *data)
691{
692 struct ap_data *ap = data;
693 struct net_device *dev = ap->local->dev;
James Ketrenosd0416742005-09-21 12:23:49 -0500694 struct ieee80211_hdr_4addr *hdr;
Al Viro8a9faf32007-12-21 03:30:16 -0500695 u16 fc, status;
696 __le16 *pos;
Jouni Malinenff1d2762005-05-12 22:54:16 -0400697 struct sta_info *sta = NULL;
698 char *txt = NULL;
699
700 if (ap->local->hostapd) {
701 dev_kfree_skb(skb);
702 return;
703 }
704
James Ketrenosd0416742005-09-21 12:23:49 -0500705 hdr = (struct ieee80211_hdr_4addr *) skb->data;
Jouni Malinenc0f72ca2005-08-14 19:08:43 -0700706 fc = le16_to_cpu(hdr->frame_ctl);
Jouni Malinen4339d322005-08-14 19:08:44 -0700707 if (WLAN_FC_GET_TYPE(fc) != IEEE80211_FTYPE_MGMT ||
708 (WLAN_FC_GET_STYPE(fc) != IEEE80211_STYPE_ASSOC_RESP &&
709 WLAN_FC_GET_STYPE(fc) != IEEE80211_STYPE_REASSOC_RESP) ||
Jouni Malinenff1d2762005-05-12 22:54:16 -0400710 skb->len < IEEE80211_MGMT_HDR_LEN + 4) {
711 printk(KERN_DEBUG "%s: hostap_ap_tx_cb_assoc received invalid "
712 "frame\n", dev->name);
713 dev_kfree_skb(skb);
714 return;
715 }
716
717 if (!ok) {
718 txt = "frame was not ACKed";
719 goto done;
720 }
721
722 spin_lock(&ap->sta_table_lock);
723 sta = ap_get_sta(ap, hdr->addr1);
724 if (sta)
725 atomic_inc(&sta->users);
726 spin_unlock(&ap->sta_table_lock);
727
728 if (!sta) {
729 txt = "STA not found";
730 goto done;
731 }
732
Al Viro8a9faf32007-12-21 03:30:16 -0500733 pos = (__le16 *) (skb->data + IEEE80211_MGMT_HDR_LEN);
Jouni Malinenff1d2762005-05-12 22:54:16 -0400734 pos++;
735 status = le16_to_cpu(*pos++);
736 if (status == WLAN_STATUS_SUCCESS) {
737 if (!(sta->flags & WLAN_STA_ASSOC))
738 hostap_event_new_sta(dev, sta);
739 txt = "STA associated";
740 sta->flags |= WLAN_STA_ASSOC;
741 sta->last_assoc = jiffies;
742 } else
743 txt = "association failed";
744
745 done:
746 if (sta)
747 atomic_dec(&sta->users);
748 if (txt) {
Johannes Berge1749612008-10-27 15:59:26 -0700749 PDEBUG(DEBUG_AP, "%s: %pM assoc_cb - %s\n",
750 dev->name, hdr->addr1, txt);
Jouni Malinenff1d2762005-05-12 22:54:16 -0400751 }
752 dev_kfree_skb(skb);
753}
754
755/* Called only as a tasklet (software IRQ); TX callback for poll frames used
756 * in verifying whether the STA is still present. */
757static void hostap_ap_tx_cb_poll(struct sk_buff *skb, int ok, void *data)
758{
759 struct ap_data *ap = data;
James Ketrenosd0416742005-09-21 12:23:49 -0500760 struct ieee80211_hdr_4addr *hdr;
Jouni Malinenff1d2762005-05-12 22:54:16 -0400761 struct sta_info *sta;
762
763 if (skb->len < 24)
764 goto fail;
James Ketrenosd0416742005-09-21 12:23:49 -0500765 hdr = (struct ieee80211_hdr_4addr *) skb->data;
Jouni Malinenff1d2762005-05-12 22:54:16 -0400766 if (ok) {
767 spin_lock(&ap->sta_table_lock);
768 sta = ap_get_sta(ap, hdr->addr1);
769 if (sta)
770 sta->flags &= ~WLAN_STA_PENDING_POLL;
771 spin_unlock(&ap->sta_table_lock);
772 } else {
Johannes Berge1749612008-10-27 15:59:26 -0700773 PDEBUG(DEBUG_AP,
774 "%s: STA %pM did not ACK activity poll frame\n",
775 ap->local->dev->name, hdr->addr1);
Jouni Malinenff1d2762005-05-12 22:54:16 -0400776 }
777
778 fail:
779 dev_kfree_skb(skb);
780}
781#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
782
783
784void hostap_init_data(local_info_t *local)
785{
786 struct ap_data *ap = local->ap;
787
788 if (ap == NULL) {
789 printk(KERN_WARNING "hostap_init_data: ap == NULL\n");
790 return;
791 }
792 memset(ap, 0, sizeof(struct ap_data));
793 ap->local = local;
794
795 ap->ap_policy = GET_INT_PARM(other_ap_policy, local->card_idx);
796 ap->bridge_packets = GET_INT_PARM(ap_bridge_packets, local->card_idx);
797 ap->max_inactivity =
798 GET_INT_PARM(ap_max_inactivity, local->card_idx) * HZ;
799 ap->autom_ap_wds = GET_INT_PARM(autom_ap_wds, local->card_idx);
800
801 spin_lock_init(&ap->sta_table_lock);
802 INIT_LIST_HEAD(&ap->sta_list);
803
804 /* Initialize task queue structure for AP management */
David Howellsc4028952006-11-22 14:57:56 +0000805 INIT_WORK(&local->ap->add_sta_proc_queue, handle_add_proc_queue);
Jouni Malinenff1d2762005-05-12 22:54:16 -0400806
807 ap->tx_callback_idx =
808 hostap_tx_callback_register(local, hostap_ap_tx_cb, ap);
809 if (ap->tx_callback_idx == 0)
810 printk(KERN_WARNING "%s: failed to register TX callback for "
811 "AP\n", local->dev->name);
812#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
David Howellsc4028952006-11-22 14:57:56 +0000813 INIT_WORK(&local->ap->wds_oper_queue, handle_wds_oper_queue);
Jouni Malinenff1d2762005-05-12 22:54:16 -0400814
815 ap->tx_callback_auth =
816 hostap_tx_callback_register(local, hostap_ap_tx_cb_auth, ap);
817 ap->tx_callback_assoc =
818 hostap_tx_callback_register(local, hostap_ap_tx_cb_assoc, ap);
819 ap->tx_callback_poll =
820 hostap_tx_callback_register(local, hostap_ap_tx_cb_poll, ap);
821 if (ap->tx_callback_auth == 0 || ap->tx_callback_assoc == 0 ||
822 ap->tx_callback_poll == 0)
823 printk(KERN_WARNING "%s: failed to register TX callback for "
824 "AP\n", local->dev->name);
825
826 spin_lock_init(&ap->mac_restrictions.lock);
827 INIT_LIST_HEAD(&ap->mac_restrictions.mac_list);
828#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
829
830 ap->initialized = 1;
831}
832
833
834void hostap_init_ap_proc(local_info_t *local)
835{
836 struct ap_data *ap = local->ap;
837
838 ap->proc = local->proc;
839 if (ap->proc == NULL)
840 return;
841
842#ifndef PRISM2_NO_PROCFS_DEBUG
843 create_proc_read_entry("ap_debug", 0, ap->proc,
844 ap_debug_proc_read, ap);
845#endif /* PRISM2_NO_PROCFS_DEBUG */
846
847#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
848 create_proc_read_entry("ap_control", 0, ap->proc,
849 ap_control_proc_read, ap);
850 create_proc_read_entry("ap", 0, ap->proc,
851 prism2_ap_proc_read, ap);
852#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
853
854}
855
856
857void hostap_free_data(struct ap_data *ap)
858{
Matthias Kaehlckec1505732007-05-28 09:38:48 -0700859 struct sta_info *n, *sta;
Jouni Malinenff1d2762005-05-12 22:54:16 -0400860
861 if (ap == NULL || !ap->initialized) {
862 printk(KERN_DEBUG "hostap_free_data: ap has not yet been "
863 "initialized - skip resource freeing\n");
864 return;
865 }
866
867#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
868 if (ap->crypt)
869 ap->crypt->deinit(ap->crypt_priv);
870 ap->crypt = ap->crypt_priv = NULL;
871#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
872
Matthias Kaehlckec1505732007-05-28 09:38:48 -0700873 list_for_each_entry_safe(sta, n, &ap->sta_list, list) {
Jouni Malinenff1d2762005-05-12 22:54:16 -0400874 ap_sta_hash_del(ap, sta);
875 list_del(&sta->list);
876 if ((sta->flags & WLAN_STA_ASSOC) && !sta->ap && sta->local)
877 hostap_event_expired_sta(sta->local->dev, sta);
878 ap_free_sta(ap, sta);
879 }
880
881#ifndef PRISM2_NO_PROCFS_DEBUG
882 if (ap->proc != NULL) {
883 remove_proc_entry("ap_debug", ap->proc);
884 }
885#endif /* PRISM2_NO_PROCFS_DEBUG */
886
887#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
888 if (ap->proc != NULL) {
889 remove_proc_entry("ap", ap->proc);
890 remove_proc_entry("ap_control", ap->proc);
891 }
892 ap_control_flush_macs(&ap->mac_restrictions);
893#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
894
895 ap->initialized = 0;
896}
897
898
899/* caller should have mutex for AP STA list handling */
900static struct sta_info* ap_get_sta(struct ap_data *ap, u8 *sta)
901{
902 struct sta_info *s;
903
904 s = ap->sta_hash[STA_HASH(sta)];
905 while (s != NULL && memcmp(s->addr, sta, ETH_ALEN) != 0)
906 s = s->hnext;
907 return s;
908}
909
910
911#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
912
913/* Called from timer handler and from scheduled AP queue handlers */
914static void prism2_send_mgmt(struct net_device *dev,
Jouni Malinen4339d322005-08-14 19:08:44 -0700915 u16 type_subtype, char *body,
Jouni Malinenff1d2762005-05-12 22:54:16 -0400916 int body_len, u8 *addr, u16 tx_cb_idx)
917{
918 struct hostap_interface *iface;
919 local_info_t *local;
James Ketrenosd0416742005-09-21 12:23:49 -0500920 struct ieee80211_hdr_4addr *hdr;
Jouni Malinenff1d2762005-05-12 22:54:16 -0400921 u16 fc;
922 struct sk_buff *skb;
923 struct hostap_skb_tx_data *meta;
924 int hdrlen;
925
926 iface = netdev_priv(dev);
927 local = iface->local;
928 dev = local->dev; /* always use master radio device */
929 iface = netdev_priv(dev);
930
931 if (!(dev->flags & IFF_UP)) {
932 PDEBUG(DEBUG_AP, "%s: prism2_send_mgmt - device is not UP - "
933 "cannot send frame\n", dev->name);
934 return;
935 }
936
937 skb = dev_alloc_skb(sizeof(*hdr) + body_len);
938 if (skb == NULL) {
939 PDEBUG(DEBUG_AP, "%s: prism2_send_mgmt failed to allocate "
940 "skb\n", dev->name);
941 return;
942 }
943
Jouni Malinen4339d322005-08-14 19:08:44 -0700944 fc = type_subtype;
Jouni Malinenff1d2762005-05-12 22:54:16 -0400945 hdrlen = hostap_80211_get_hdrlen(fc);
James Ketrenosd0416742005-09-21 12:23:49 -0500946 hdr = (struct ieee80211_hdr_4addr *) skb_put(skb, hdrlen);
Jouni Malinenff1d2762005-05-12 22:54:16 -0400947 if (body)
948 memcpy(skb_put(skb, body_len), body, body_len);
949
950 memset(hdr, 0, hdrlen);
951
952 /* FIX: ctrl::ack sending used special HFA384X_TX_CTRL_802_11
953 * tx_control instead of using local->tx_control */
954
955
956 memcpy(hdr->addr1, addr, ETH_ALEN); /* DA / RA */
Jouni Malinen4339d322005-08-14 19:08:44 -0700957 if (WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA) {
Jouni Malinenb2f4a2e2005-08-14 21:00:01 -0700958 fc |= IEEE80211_FCTL_FROMDS;
Jouni Malinenff1d2762005-05-12 22:54:16 -0400959 memcpy(hdr->addr2, dev->dev_addr, ETH_ALEN); /* BSSID */
960 memcpy(hdr->addr3, dev->dev_addr, ETH_ALEN); /* SA */
Jouni Malinen4339d322005-08-14 19:08:44 -0700961 } else if (WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_CTL) {
Jouni Malinenff1d2762005-05-12 22:54:16 -0400962 /* control:ACK does not have addr2 or addr3 */
963 memset(hdr->addr2, 0, ETH_ALEN);
964 memset(hdr->addr3, 0, ETH_ALEN);
965 } else {
966 memcpy(hdr->addr2, dev->dev_addr, ETH_ALEN); /* SA */
967 memcpy(hdr->addr3, dev->dev_addr, ETH_ALEN); /* BSSID */
968 }
969
Jouni Malinenc0f72ca2005-08-14 19:08:43 -0700970 hdr->frame_ctl = cpu_to_le16(fc);
Jouni Malinenff1d2762005-05-12 22:54:16 -0400971
972 meta = (struct hostap_skb_tx_data *) skb->cb;
973 memset(meta, 0, sizeof(*meta));
974 meta->magic = HOSTAP_SKB_TX_DATA_MAGIC;
975 meta->iface = iface;
976 meta->tx_cb_idx = tx_cb_idx;
977
978 skb->dev = dev;
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700979 skb_reset_mac_header(skb);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700980 skb_reset_network_header(skb);
Jouni Malinenff1d2762005-05-12 22:54:16 -0400981 dev_queue_xmit(skb);
982}
983#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
984
985
986static int prism2_sta_proc_read(char *page, char **start, off_t off,
987 int count, int *eof, void *data)
988{
989 char *p = page;
990 struct sta_info *sta = (struct sta_info *) data;
991 int i;
992
993 /* FIX: possible race condition.. the STA data could have just expired,
994 * but proc entry was still here so that the read could have started;
995 * some locking should be done here.. */
996
997 if (off != 0) {
998 *eof = 1;
999 return 0;
1000 }
1001
Johannes Berge1749612008-10-27 15:59:26 -07001002 p += sprintf(p, "%s=%pM\nusers=%d\naid=%d\n"
Jouni Malinenff1d2762005-05-12 22:54:16 -04001003 "flags=0x%04x%s%s%s%s%s%s%s\n"
1004 "capability=0x%02x\nlisten_interval=%d\nsupported_rates=",
1005 sta->ap ? "AP" : "STA",
Johannes Berge1749612008-10-27 15:59:26 -07001006 sta->addr, atomic_read(&sta->users), sta->aid,
Jouni Malinenff1d2762005-05-12 22:54:16 -04001007 sta->flags,
1008 sta->flags & WLAN_STA_AUTH ? " AUTH" : "",
1009 sta->flags & WLAN_STA_ASSOC ? " ASSOC" : "",
1010 sta->flags & WLAN_STA_PS ? " PS" : "",
1011 sta->flags & WLAN_STA_TIM ? " TIM" : "",
1012 sta->flags & WLAN_STA_PERM ? " PERM" : "",
1013 sta->flags & WLAN_STA_AUTHORIZED ? " AUTHORIZED" : "",
1014 sta->flags & WLAN_STA_PENDING_POLL ? " POLL" : "",
1015 sta->capability, sta->listen_interval);
1016 /* supported_rates: 500 kbit/s units with msb ignored */
1017 for (i = 0; i < sizeof(sta->supported_rates); i++)
1018 if (sta->supported_rates[i] != 0)
1019 p += sprintf(p, "%d%sMbps ",
1020 (sta->supported_rates[i] & 0x7f) / 2,
1021 sta->supported_rates[i] & 1 ? ".5" : "");
1022 p += sprintf(p, "\njiffies=%lu\nlast_auth=%lu\nlast_assoc=%lu\n"
1023 "last_rx=%lu\nlast_tx=%lu\nrx_packets=%lu\n"
1024 "tx_packets=%lu\n"
1025 "rx_bytes=%lu\ntx_bytes=%lu\nbuffer_count=%d\n"
1026 "last_rx: silence=%d dBm signal=%d dBm rate=%d%s Mbps\n"
1027 "tx_rate=%d\ntx[1M]=%d\ntx[2M]=%d\ntx[5.5M]=%d\n"
1028 "tx[11M]=%d\n"
1029 "rx[1M]=%d\nrx[2M]=%d\nrx[5.5M]=%d\nrx[11M]=%d\n",
1030 jiffies, sta->last_auth, sta->last_assoc, sta->last_rx,
1031 sta->last_tx,
1032 sta->rx_packets, sta->tx_packets, sta->rx_bytes,
1033 sta->tx_bytes, skb_queue_len(&sta->tx_buf),
1034 sta->last_rx_silence,
1035 sta->last_rx_signal, sta->last_rx_rate / 10,
1036 sta->last_rx_rate % 10 ? ".5" : "",
1037 sta->tx_rate, sta->tx_count[0], sta->tx_count[1],
1038 sta->tx_count[2], sta->tx_count[3], sta->rx_count[0],
1039 sta->rx_count[1], sta->rx_count[2], sta->rx_count[3]);
1040 if (sta->crypt && sta->crypt->ops && sta->crypt->ops->print_stats)
1041 p = sta->crypt->ops->print_stats(p, sta->crypt->priv);
1042#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
1043 if (sta->ap) {
1044 if (sta->u.ap.channel >= 0)
1045 p += sprintf(p, "channel=%d\n", sta->u.ap.channel);
1046 p += sprintf(p, "ssid=");
1047 for (i = 0; i < sta->u.ap.ssid_len; i++)
1048 p += sprintf(p, ((sta->u.ap.ssid[i] >= 32 &&
1049 sta->u.ap.ssid[i] < 127) ?
1050 "%c" : "<%02x>"),
1051 sta->u.ap.ssid[i]);
1052 p += sprintf(p, "\n");
1053 }
1054#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
1055
1056 return (p - page);
1057}
1058
1059
David Howellsc4028952006-11-22 14:57:56 +00001060static void handle_add_proc_queue(struct work_struct *work)
Jouni Malinenff1d2762005-05-12 22:54:16 -04001061{
David Howellsc4028952006-11-22 14:57:56 +00001062 struct ap_data *ap = container_of(work, struct ap_data,
1063 add_sta_proc_queue);
Jouni Malinenff1d2762005-05-12 22:54:16 -04001064 struct sta_info *sta;
1065 char name[20];
1066 struct add_sta_proc_data *entry, *prev;
1067
1068 entry = ap->add_sta_proc_entries;
1069 ap->add_sta_proc_entries = NULL;
1070
1071 while (entry) {
1072 spin_lock_bh(&ap->sta_table_lock);
1073 sta = ap_get_sta(ap, entry->addr);
1074 if (sta)
1075 atomic_inc(&sta->users);
1076 spin_unlock_bh(&ap->sta_table_lock);
1077
1078 if (sta) {
Johannes Berge1749612008-10-27 15:59:26 -07001079 sprintf(name, "%pM", sta->addr);
Jouni Malinenff1d2762005-05-12 22:54:16 -04001080 sta->proc = create_proc_read_entry(
1081 name, 0, ap->proc,
1082 prism2_sta_proc_read, sta);
1083
1084 atomic_dec(&sta->users);
1085 }
1086
1087 prev = entry;
1088 entry = entry->next;
1089 kfree(prev);
1090 }
1091}
1092
1093
1094static struct sta_info * ap_add_sta(struct ap_data *ap, u8 *addr)
1095{
1096 struct sta_info *sta;
1097
Yan Burmanb0471bb72006-12-02 13:33:40 +02001098 sta = kzalloc(sizeof(struct sta_info), GFP_ATOMIC);
Jouni Malinenff1d2762005-05-12 22:54:16 -04001099 if (sta == NULL) {
1100 PDEBUG(DEBUG_AP, "AP: kmalloc failed\n");
1101 return NULL;
1102 }
1103
1104 /* initialize STA info data */
Jouni Malinenff1d2762005-05-12 22:54:16 -04001105 sta->local = ap->local;
1106 skb_queue_head_init(&sta->tx_buf);
1107 memcpy(sta->addr, addr, ETH_ALEN);
1108
1109 atomic_inc(&sta->users);
1110 spin_lock_bh(&ap->sta_table_lock);
1111 list_add(&sta->list, &ap->sta_list);
1112 ap->num_sta++;
1113 ap_sta_hash_add(ap, sta);
1114 spin_unlock_bh(&ap->sta_table_lock);
1115
1116 if (ap->proc) {
1117 struct add_sta_proc_data *entry;
1118 /* schedule a non-interrupt context process to add a procfs
1119 * entry for the STA since procfs code use GFP_KERNEL */
1120 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
1121 if (entry) {
1122 memcpy(entry->addr, sta->addr, ETH_ALEN);
1123 entry->next = ap->add_sta_proc_entries;
1124 ap->add_sta_proc_entries = entry;
1125 schedule_work(&ap->add_sta_proc_queue);
1126 } else
1127 printk(KERN_DEBUG "Failed to add STA proc data\n");
1128 }
1129
1130#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
1131 init_timer(&sta->timer);
1132 sta->timer.expires = jiffies + ap->max_inactivity;
1133 sta->timer.data = (unsigned long) sta;
1134 sta->timer.function = ap_handle_timer;
1135 if (!ap->local->hostapd)
1136 add_timer(&sta->timer);
1137#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
1138
1139 return sta;
1140}
1141
1142
1143static int ap_tx_rate_ok(int rateidx, struct sta_info *sta,
1144 local_info_t *local)
1145{
1146 if (rateidx > sta->tx_max_rate ||
1147 !(sta->tx_supp_rates & (1 << rateidx)))
1148 return 0;
1149
1150 if (local->tx_rate_control != 0 &&
1151 !(local->tx_rate_control & (1 << rateidx)))
1152 return 0;
1153
1154 return 1;
1155}
1156
1157
1158static void prism2_check_tx_rates(struct sta_info *sta)
1159{
1160 int i;
1161
1162 sta->tx_supp_rates = 0;
1163 for (i = 0; i < sizeof(sta->supported_rates); i++) {
1164 if ((sta->supported_rates[i] & 0x7f) == 2)
1165 sta->tx_supp_rates |= WLAN_RATE_1M;
1166 if ((sta->supported_rates[i] & 0x7f) == 4)
1167 sta->tx_supp_rates |= WLAN_RATE_2M;
1168 if ((sta->supported_rates[i] & 0x7f) == 11)
1169 sta->tx_supp_rates |= WLAN_RATE_5M5;
1170 if ((sta->supported_rates[i] & 0x7f) == 22)
1171 sta->tx_supp_rates |= WLAN_RATE_11M;
1172 }
1173 sta->tx_max_rate = sta->tx_rate = sta->tx_rate_idx = 0;
1174 if (sta->tx_supp_rates & WLAN_RATE_1M) {
1175 sta->tx_max_rate = 0;
1176 if (ap_tx_rate_ok(0, sta, sta->local)) {
1177 sta->tx_rate = 10;
1178 sta->tx_rate_idx = 0;
1179 }
1180 }
1181 if (sta->tx_supp_rates & WLAN_RATE_2M) {
1182 sta->tx_max_rate = 1;
1183 if (ap_tx_rate_ok(1, sta, sta->local)) {
1184 sta->tx_rate = 20;
1185 sta->tx_rate_idx = 1;
1186 }
1187 }
1188 if (sta->tx_supp_rates & WLAN_RATE_5M5) {
1189 sta->tx_max_rate = 2;
1190 if (ap_tx_rate_ok(2, sta, sta->local)) {
1191 sta->tx_rate = 55;
1192 sta->tx_rate_idx = 2;
1193 }
1194 }
1195 if (sta->tx_supp_rates & WLAN_RATE_11M) {
1196 sta->tx_max_rate = 3;
1197 if (ap_tx_rate_ok(3, sta, sta->local)) {
1198 sta->tx_rate = 110;
1199 sta->tx_rate_idx = 3;
1200 }
1201 }
1202}
1203
1204
1205#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
1206
1207static void ap_crypt_init(struct ap_data *ap)
1208{
John W. Linville274bfb82008-10-29 11:35:05 -04001209 ap->crypt = lib80211_get_crypto_ops("WEP");
Jouni Malinenff1d2762005-05-12 22:54:16 -04001210
1211 if (ap->crypt) {
1212 if (ap->crypt->init) {
1213 ap->crypt_priv = ap->crypt->init(0);
1214 if (ap->crypt_priv == NULL)
1215 ap->crypt = NULL;
1216 else {
1217 u8 key[WEP_KEY_LEN];
1218 get_random_bytes(key, WEP_KEY_LEN);
1219 ap->crypt->set_key(key, WEP_KEY_LEN, NULL,
1220 ap->crypt_priv);
1221 }
1222 }
1223 }
1224
1225 if (ap->crypt == NULL) {
1226 printk(KERN_WARNING "AP could not initialize WEP: load module "
John W. Linville274bfb82008-10-29 11:35:05 -04001227 "lib80211_crypt_wep.ko\n");
Jouni Malinenff1d2762005-05-12 22:54:16 -04001228 }
1229}
1230
1231
1232/* Generate challenge data for shared key authentication. IEEE 802.11 specifies
1233 * that WEP algorithm is used for generating challange. This should be unique,
1234 * but otherwise there is not really need for randomness etc. Initialize WEP
1235 * with pseudo random key and then use increasing IV to get unique challenge
1236 * streams.
1237 *
1238 * Called only as a scheduled task for pending AP frames.
1239 */
1240static char * ap_auth_make_challenge(struct ap_data *ap)
1241{
1242 char *tmpbuf;
1243 struct sk_buff *skb;
1244
1245 if (ap->crypt == NULL) {
1246 ap_crypt_init(ap);
1247 if (ap->crypt == NULL)
1248 return NULL;
1249 }
1250
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001251 tmpbuf = kmalloc(WLAN_AUTH_CHALLENGE_LEN, GFP_ATOMIC);
Jouni Malinenff1d2762005-05-12 22:54:16 -04001252 if (tmpbuf == NULL) {
1253 PDEBUG(DEBUG_AP, "AP: kmalloc failed for challenge\n");
1254 return NULL;
1255 }
1256
1257 skb = dev_alloc_skb(WLAN_AUTH_CHALLENGE_LEN +
James Ketrenos5bfc8192005-09-21 12:23:51 -05001258 ap->crypt->extra_mpdu_prefix_len +
1259 ap->crypt->extra_mpdu_postfix_len);
Jouni Malinenff1d2762005-05-12 22:54:16 -04001260 if (skb == NULL) {
1261 kfree(tmpbuf);
1262 return NULL;
1263 }
1264
James Ketrenos5bfc8192005-09-21 12:23:51 -05001265 skb_reserve(skb, ap->crypt->extra_mpdu_prefix_len);
Jouni Malinenff1d2762005-05-12 22:54:16 -04001266 memset(skb_put(skb, WLAN_AUTH_CHALLENGE_LEN), 0,
1267 WLAN_AUTH_CHALLENGE_LEN);
1268 if (ap->crypt->encrypt_mpdu(skb, 0, ap->crypt_priv)) {
1269 dev_kfree_skb(skb);
1270 kfree(tmpbuf);
1271 return NULL;
1272 }
1273
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001274 skb_copy_from_linear_data_offset(skb, ap->crypt->extra_mpdu_prefix_len,
1275 tmpbuf, WLAN_AUTH_CHALLENGE_LEN);
Jouni Malinenff1d2762005-05-12 22:54:16 -04001276 dev_kfree_skb(skb);
1277
1278 return tmpbuf;
1279}
1280
1281
1282/* Called only as a scheduled task for pending AP frames. */
1283static void handle_authen(local_info_t *local, struct sk_buff *skb,
1284 struct hostap_80211_rx_status *rx_stats)
1285{
1286 struct net_device *dev = local->dev;
James Ketrenosd0416742005-09-21 12:23:49 -05001287 struct ieee80211_hdr_4addr *hdr = (struct ieee80211_hdr_4addr *) skb->data;
Jouni Malinenff1d2762005-05-12 22:54:16 -04001288 size_t hdrlen;
1289 struct ap_data *ap = local->ap;
1290 char body[8 + WLAN_AUTH_CHALLENGE_LEN], *challenge = NULL;
1291 int len, olen;
Al Viro8a9faf32007-12-21 03:30:16 -05001292 u16 auth_alg, auth_transaction, status_code;
1293 __le16 *pos;
Jouni Malinenff1d2762005-05-12 22:54:16 -04001294 u16 resp = WLAN_STATUS_SUCCESS, fc;
1295 struct sta_info *sta = NULL;
John W. Linville274bfb82008-10-29 11:35:05 -04001296 struct lib80211_crypt_data *crypt;
Jouni Malinenff1d2762005-05-12 22:54:16 -04001297 char *txt = "";
1298
1299 len = skb->len - IEEE80211_MGMT_HDR_LEN;
1300
Jouni Malinenc0f72ca2005-08-14 19:08:43 -07001301 fc = le16_to_cpu(hdr->frame_ctl);
Jouni Malinenff1d2762005-05-12 22:54:16 -04001302 hdrlen = hostap_80211_get_hdrlen(fc);
1303
1304 if (len < 6) {
1305 PDEBUG(DEBUG_AP, "%s: handle_authen - too short payload "
Johannes Berge1749612008-10-27 15:59:26 -07001306 "(len=%d) from %pM\n", dev->name, len, hdr->addr2);
Jouni Malinenff1d2762005-05-12 22:54:16 -04001307 return;
1308 }
1309
1310 spin_lock_bh(&local->ap->sta_table_lock);
1311 sta = ap_get_sta(local->ap, hdr->addr2);
1312 if (sta)
1313 atomic_inc(&sta->users);
1314 spin_unlock_bh(&local->ap->sta_table_lock);
1315
1316 if (sta && sta->crypt)
1317 crypt = sta->crypt;
1318 else {
1319 int idx = 0;
1320 if (skb->len >= hdrlen + 3)
1321 idx = skb->data[hdrlen + 3] >> 6;
John W. Linville274bfb82008-10-29 11:35:05 -04001322 crypt = local->crypt_info.crypt[idx];
Jouni Malinenff1d2762005-05-12 22:54:16 -04001323 }
1324
Al Viro8a9faf32007-12-21 03:30:16 -05001325 pos = (__le16 *) (skb->data + IEEE80211_MGMT_HDR_LEN);
Jouni Malinenff1d2762005-05-12 22:54:16 -04001326 auth_alg = __le16_to_cpu(*pos);
1327 pos++;
1328 auth_transaction = __le16_to_cpu(*pos);
1329 pos++;
1330 status_code = __le16_to_cpu(*pos);
1331 pos++;
1332
1333 if (memcmp(dev->dev_addr, hdr->addr2, ETH_ALEN) == 0 ||
1334 ap_control_mac_deny(&ap->mac_restrictions, hdr->addr2)) {
1335 txt = "authentication denied";
1336 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1337 goto fail;
1338 }
1339
1340 if (((local->auth_algs & PRISM2_AUTH_OPEN) &&
1341 auth_alg == WLAN_AUTH_OPEN) ||
1342 ((local->auth_algs & PRISM2_AUTH_SHARED_KEY) &&
1343 crypt && auth_alg == WLAN_AUTH_SHARED_KEY)) {
1344 } else {
1345 txt = "unsupported algorithm";
1346 resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
1347 goto fail;
1348 }
1349
1350 if (len >= 8) {
1351 u8 *u = (u8 *) pos;
1352 if (*u == WLAN_EID_CHALLENGE) {
1353 if (*(u + 1) != WLAN_AUTH_CHALLENGE_LEN) {
1354 txt = "invalid challenge len";
1355 resp = WLAN_STATUS_CHALLENGE_FAIL;
1356 goto fail;
1357 }
1358 if (len - 8 < WLAN_AUTH_CHALLENGE_LEN) {
1359 txt = "challenge underflow";
1360 resp = WLAN_STATUS_CHALLENGE_FAIL;
1361 goto fail;
1362 }
1363 challenge = (char *) (u + 2);
1364 }
1365 }
1366
1367 if (sta && sta->ap) {
1368 if (time_after(jiffies, sta->u.ap.last_beacon +
1369 (10 * sta->listen_interval * HZ) / 1024)) {
1370 PDEBUG(DEBUG_AP, "%s: no beacons received for a while,"
Johannes Berge1749612008-10-27 15:59:26 -07001371 " assuming AP %pM is now STA\n",
1372 dev->name, sta->addr);
Jouni Malinenff1d2762005-05-12 22:54:16 -04001373 sta->ap = 0;
1374 sta->flags = 0;
1375 sta->u.sta.challenge = NULL;
1376 } else {
1377 txt = "AP trying to authenticate?";
1378 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1379 goto fail;
1380 }
1381 }
1382
1383 if ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 1) ||
1384 (auth_alg == WLAN_AUTH_SHARED_KEY &&
1385 (auth_transaction == 1 ||
1386 (auth_transaction == 3 && sta != NULL &&
1387 sta->u.sta.challenge != NULL)))) {
1388 } else {
1389 txt = "unknown authentication transaction number";
1390 resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
1391 goto fail;
1392 }
1393
1394 if (sta == NULL) {
1395 txt = "new STA";
1396
1397 if (local->ap->num_sta >= MAX_STA_COUNT) {
1398 /* FIX: might try to remove some old STAs first? */
1399 txt = "no more room for new STAs";
1400 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1401 goto fail;
1402 }
1403
1404 sta = ap_add_sta(local->ap, hdr->addr2);
1405 if (sta == NULL) {
1406 txt = "ap_add_sta failed";
1407 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1408 goto fail;
1409 }
1410 }
1411
1412 switch (auth_alg) {
1413 case WLAN_AUTH_OPEN:
1414 txt = "authOK";
1415 /* IEEE 802.11 standard is not completely clear about
1416 * whether STA is considered authenticated after
1417 * authentication OK frame has been send or after it
1418 * has been ACKed. In order to reduce interoperability
1419 * issues, mark the STA authenticated before ACK. */
1420 sta->flags |= WLAN_STA_AUTH;
1421 break;
1422
1423 case WLAN_AUTH_SHARED_KEY:
1424 if (auth_transaction == 1) {
1425 if (sta->u.sta.challenge == NULL) {
1426 sta->u.sta.challenge =
1427 ap_auth_make_challenge(local->ap);
1428 if (sta->u.sta.challenge == NULL) {
1429 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1430 goto fail;
1431 }
1432 }
1433 } else {
1434 if (sta->u.sta.challenge == NULL ||
1435 challenge == NULL ||
1436 memcmp(sta->u.sta.challenge, challenge,
1437 WLAN_AUTH_CHALLENGE_LEN) != 0 ||
Jeff Garzik831a1792005-08-25 20:59:10 -04001438 !(fc & IEEE80211_FCTL_PROTECTED)) {
Jouni Malinenff1d2762005-05-12 22:54:16 -04001439 txt = "challenge response incorrect";
1440 resp = WLAN_STATUS_CHALLENGE_FAIL;
1441 goto fail;
1442 }
1443
1444 txt = "challenge OK - authOK";
1445 /* IEEE 802.11 standard is not completely clear about
1446 * whether STA is considered authenticated after
1447 * authentication OK frame has been send or after it
1448 * has been ACKed. In order to reduce interoperability
1449 * issues, mark the STA authenticated before ACK. */
1450 sta->flags |= WLAN_STA_AUTH;
1451 kfree(sta->u.sta.challenge);
1452 sta->u.sta.challenge = NULL;
1453 }
1454 break;
1455 }
1456
1457 fail:
Al Viro8a9faf32007-12-21 03:30:16 -05001458 pos = (__le16 *) body;
Jouni Malinenff1d2762005-05-12 22:54:16 -04001459 *pos = cpu_to_le16(auth_alg);
1460 pos++;
1461 *pos = cpu_to_le16(auth_transaction + 1);
1462 pos++;
1463 *pos = cpu_to_le16(resp); /* status_code */
1464 pos++;
1465 olen = 6;
1466
1467 if (resp == WLAN_STATUS_SUCCESS && sta != NULL &&
1468 sta->u.sta.challenge != NULL &&
1469 auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 1) {
1470 u8 *tmp = (u8 *) pos;
1471 *tmp++ = WLAN_EID_CHALLENGE;
1472 *tmp++ = WLAN_AUTH_CHALLENGE_LEN;
1473 pos++;
1474 memcpy(pos, sta->u.sta.challenge, WLAN_AUTH_CHALLENGE_LEN);
1475 olen += 2 + WLAN_AUTH_CHALLENGE_LEN;
1476 }
1477
Jouni Malinen4339d322005-08-14 19:08:44 -07001478 prism2_send_mgmt(dev, IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_AUTH,
Jouni Malinenff1d2762005-05-12 22:54:16 -04001479 body, olen, hdr->addr2, ap->tx_callback_auth);
1480
1481 if (sta) {
1482 sta->last_rx = jiffies;
1483 atomic_dec(&sta->users);
1484 }
1485
1486 if (resp) {
Johannes Berge1749612008-10-27 15:59:26 -07001487 PDEBUG(DEBUG_AP, "%s: %pM auth (alg=%d "
Joe Perches0795af52007-10-03 17:59:30 -07001488 "trans#=%d stat=%d len=%d fc=%04x) ==> %d (%s)\n",
Johannes Berge1749612008-10-27 15:59:26 -07001489 dev->name, hdr->addr2,
David S. Miller21f644f2008-04-08 16:50:44 -07001490 auth_alg, auth_transaction, status_code, len,
1491 fc, resp, txt);
Jouni Malinenff1d2762005-05-12 22:54:16 -04001492 }
1493}
1494
1495
1496/* Called only as a scheduled task for pending AP frames. */
1497static void handle_assoc(local_info_t *local, struct sk_buff *skb,
1498 struct hostap_80211_rx_status *rx_stats, int reassoc)
1499{
1500 struct net_device *dev = local->dev;
James Ketrenosd0416742005-09-21 12:23:49 -05001501 struct ieee80211_hdr_4addr *hdr = (struct ieee80211_hdr_4addr *) skb->data;
Jouni Malinenff1d2762005-05-12 22:54:16 -04001502 char body[12], *p, *lpos;
1503 int len, left;
Al Viro8a9faf32007-12-21 03:30:16 -05001504 __le16 *pos;
Jouni Malinenff1d2762005-05-12 22:54:16 -04001505 u16 resp = WLAN_STATUS_SUCCESS;
1506 struct sta_info *sta = NULL;
1507 int send_deauth = 0;
1508 char *txt = "";
1509 u8 prev_ap[ETH_ALEN];
1510
1511 left = len = skb->len - IEEE80211_MGMT_HDR_LEN;
1512
1513 if (len < (reassoc ? 10 : 4)) {
1514 PDEBUG(DEBUG_AP, "%s: handle_assoc - too short payload "
Johannes Berge1749612008-10-27 15:59:26 -07001515 "(len=%d, reassoc=%d) from %pM\n",
1516 dev->name, len, reassoc, hdr->addr2);
Jouni Malinenff1d2762005-05-12 22:54:16 -04001517 return;
1518 }
1519
1520 spin_lock_bh(&local->ap->sta_table_lock);
1521 sta = ap_get_sta(local->ap, hdr->addr2);
1522 if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
1523 spin_unlock_bh(&local->ap->sta_table_lock);
1524 txt = "trying to associate before authentication";
1525 send_deauth = 1;
1526 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1527 sta = NULL; /* do not decrement sta->users */
1528 goto fail;
1529 }
1530 atomic_inc(&sta->users);
1531 spin_unlock_bh(&local->ap->sta_table_lock);
1532
Al Viro8a9faf32007-12-21 03:30:16 -05001533 pos = (__le16 *) (skb->data + IEEE80211_MGMT_HDR_LEN);
Jouni Malinenff1d2762005-05-12 22:54:16 -04001534 sta->capability = __le16_to_cpu(*pos);
1535 pos++; left -= 2;
1536 sta->listen_interval = __le16_to_cpu(*pos);
1537 pos++; left -= 2;
1538
1539 if (reassoc) {
1540 memcpy(prev_ap, pos, ETH_ALEN);
1541 pos++; pos++; pos++; left -= 6;
1542 } else
1543 memset(prev_ap, 0, ETH_ALEN);
1544
1545 if (left >= 2) {
1546 unsigned int ileft;
1547 unsigned char *u = (unsigned char *) pos;
1548
1549 if (*u == WLAN_EID_SSID) {
1550 u++; left--;
1551 ileft = *u;
1552 u++; left--;
1553
1554 if (ileft > left || ileft > MAX_SSID_LEN) {
1555 txt = "SSID overflow";
1556 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1557 goto fail;
1558 }
1559
1560 if (ileft != strlen(local->essid) ||
1561 memcmp(local->essid, u, ileft) != 0) {
1562 txt = "not our SSID";
1563 resp = WLAN_STATUS_ASSOC_DENIED_UNSPEC;
1564 goto fail;
1565 }
1566
1567 u += ileft;
1568 left -= ileft;
1569 }
1570
1571 if (left >= 2 && *u == WLAN_EID_SUPP_RATES) {
1572 u++; left--;
1573 ileft = *u;
1574 u++; left--;
Jeff Garzik74fae822005-07-31 13:08:32 -04001575
Jouni Malinenff1d2762005-05-12 22:54:16 -04001576 if (ileft > left || ileft == 0 ||
1577 ileft > WLAN_SUPP_RATES_MAX) {
1578 txt = "SUPP_RATES len error";
1579 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1580 goto fail;
1581 }
1582
1583 memset(sta->supported_rates, 0,
1584 sizeof(sta->supported_rates));
1585 memcpy(sta->supported_rates, u, ileft);
1586 prism2_check_tx_rates(sta);
1587
1588 u += ileft;
1589 left -= ileft;
1590 }
1591
1592 if (left > 0) {
Johannes Berge1749612008-10-27 15:59:26 -07001593 PDEBUG(DEBUG_AP, "%s: assoc from %pM"
Joe Perches0795af52007-10-03 17:59:30 -07001594 " with extra data (%d bytes) [",
Johannes Berge1749612008-10-27 15:59:26 -07001595 dev->name, hdr->addr2, left);
Jouni Malinenff1d2762005-05-12 22:54:16 -04001596 while (left > 0) {
1597 PDEBUG2(DEBUG_AP, "<%02x>", *u);
1598 u++; left--;
1599 }
1600 PDEBUG2(DEBUG_AP, "]\n");
1601 }
1602 } else {
1603 txt = "frame underflow";
1604 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1605 goto fail;
1606 }
1607
1608 /* get a unique AID */
1609 if (sta->aid > 0)
1610 txt = "OK, old AID";
1611 else {
1612 spin_lock_bh(&local->ap->sta_table_lock);
1613 for (sta->aid = 1; sta->aid <= MAX_AID_TABLE_SIZE; sta->aid++)
1614 if (local->ap->sta_aid[sta->aid - 1] == NULL)
1615 break;
1616 if (sta->aid > MAX_AID_TABLE_SIZE) {
1617 sta->aid = 0;
1618 spin_unlock_bh(&local->ap->sta_table_lock);
1619 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
1620 txt = "no room for more AIDs";
1621 } else {
1622 local->ap->sta_aid[sta->aid - 1] = sta;
1623 spin_unlock_bh(&local->ap->sta_table_lock);
1624 txt = "OK, new AID";
1625 }
1626 }
1627
1628 fail:
Al Viro8a9faf32007-12-21 03:30:16 -05001629 pos = (__le16 *) body;
Jouni Malinenff1d2762005-05-12 22:54:16 -04001630
1631 if (send_deauth) {
Al Viro8a9faf32007-12-21 03:30:16 -05001632 *pos = cpu_to_le16(WLAN_REASON_STA_REQ_ASSOC_WITHOUT_AUTH);
Jouni Malinenff1d2762005-05-12 22:54:16 -04001633 pos++;
1634 } else {
1635 /* FIX: CF-Pollable and CF-PollReq should be set to match the
1636 * values in beacons/probe responses */
1637 /* FIX: how about privacy and WEP? */
1638 /* capability */
Al Viro8a9faf32007-12-21 03:30:16 -05001639 *pos = cpu_to_le16(WLAN_CAPABILITY_ESS);
Jouni Malinenff1d2762005-05-12 22:54:16 -04001640 pos++;
1641
1642 /* status_code */
Al Viro8a9faf32007-12-21 03:30:16 -05001643 *pos = cpu_to_le16(resp);
Jouni Malinenff1d2762005-05-12 22:54:16 -04001644 pos++;
1645
Al Viro8a9faf32007-12-21 03:30:16 -05001646 *pos = cpu_to_le16((sta && sta->aid > 0 ? sta->aid : 0) |
Jouni Malinenff1d2762005-05-12 22:54:16 -04001647 BIT(14) | BIT(15)); /* AID */
1648 pos++;
1649
1650 /* Supported rates (Information element) */
1651 p = (char *) pos;
1652 *p++ = WLAN_EID_SUPP_RATES;
1653 lpos = p;
1654 *p++ = 0; /* len */
1655 if (local->tx_rate_control & WLAN_RATE_1M) {
1656 *p++ = local->basic_rates & WLAN_RATE_1M ? 0x82 : 0x02;
1657 (*lpos)++;
1658 }
1659 if (local->tx_rate_control & WLAN_RATE_2M) {
1660 *p++ = local->basic_rates & WLAN_RATE_2M ? 0x84 : 0x04;
1661 (*lpos)++;
1662 }
1663 if (local->tx_rate_control & WLAN_RATE_5M5) {
1664 *p++ = local->basic_rates & WLAN_RATE_5M5 ?
1665 0x8b : 0x0b;
1666 (*lpos)++;
1667 }
1668 if (local->tx_rate_control & WLAN_RATE_11M) {
1669 *p++ = local->basic_rates & WLAN_RATE_11M ?
1670 0x96 : 0x16;
1671 (*lpos)++;
1672 }
Al Viro8a9faf32007-12-21 03:30:16 -05001673 pos = (__le16 *) p;
Jouni Malinenff1d2762005-05-12 22:54:16 -04001674 }
1675
Jouni Malinen4339d322005-08-14 19:08:44 -07001676 prism2_send_mgmt(dev, IEEE80211_FTYPE_MGMT |
1677 (send_deauth ? IEEE80211_STYPE_DEAUTH :
1678 (reassoc ? IEEE80211_STYPE_REASSOC_RESP :
1679 IEEE80211_STYPE_ASSOC_RESP)),
Jouni Malinenff1d2762005-05-12 22:54:16 -04001680 body, (u8 *) pos - (u8 *) body,
1681 hdr->addr2,
1682 send_deauth ? 0 : local->ap->tx_callback_assoc);
1683
1684 if (sta) {
1685 if (resp == WLAN_STATUS_SUCCESS) {
1686 sta->last_rx = jiffies;
1687 /* STA will be marked associated from TX callback, if
1688 * AssocResp is ACKed */
1689 }
1690 atomic_dec(&sta->users);
1691 }
1692
1693#if 0
Johannes Berge1749612008-10-27 15:59:26 -07001694 PDEBUG(DEBUG_AP, "%s: %pM %sassoc (len=%d "
1695 "prev_ap=%pM) => %d(%d) (%s)\n",
David S. Miller21f644f2008-04-08 16:50:44 -07001696 dev->name,
Johannes Berge1749612008-10-27 15:59:26 -07001697 hdr->addr2,
David S. Miller21f644f2008-04-08 16:50:44 -07001698 reassoc ? "re" : "", len,
Johannes Berge1749612008-10-27 15:59:26 -07001699 prev_ap,
David S. Miller21f644f2008-04-08 16:50:44 -07001700 resp, send_deauth, txt);
Jouni Malinenff1d2762005-05-12 22:54:16 -04001701#endif
1702}
1703
1704
1705/* Called only as a scheduled task for pending AP frames. */
1706static void handle_deauth(local_info_t *local, struct sk_buff *skb,
1707 struct hostap_80211_rx_status *rx_stats)
1708{
1709 struct net_device *dev = local->dev;
James Ketrenosd0416742005-09-21 12:23:49 -05001710 struct ieee80211_hdr_4addr *hdr = (struct ieee80211_hdr_4addr *) skb->data;
Jouni Malinenff1d2762005-05-12 22:54:16 -04001711 char *body = (char *) (skb->data + IEEE80211_MGMT_HDR_LEN);
1712 int len;
Al Viro8a9faf32007-12-21 03:30:16 -05001713 u16 reason_code;
1714 __le16 *pos;
Jouni Malinenff1d2762005-05-12 22:54:16 -04001715 struct sta_info *sta = NULL;
1716
1717 len = skb->len - IEEE80211_MGMT_HDR_LEN;
1718
1719 if (len < 2) {
1720 printk("handle_deauth - too short payload (len=%d)\n", len);
1721 return;
1722 }
1723
Al Viro8a9faf32007-12-21 03:30:16 -05001724 pos = (__le16 *) body;
1725 reason_code = le16_to_cpu(*pos);
Jouni Malinenff1d2762005-05-12 22:54:16 -04001726
Johannes Berge1749612008-10-27 15:59:26 -07001727 PDEBUG(DEBUG_AP, "%s: deauthentication: %pM len=%d, "
1728 "reason_code=%d\n", dev->name, hdr->addr2,
David S. Miller21f644f2008-04-08 16:50:44 -07001729 len, reason_code);
Jouni Malinenff1d2762005-05-12 22:54:16 -04001730
1731 spin_lock_bh(&local->ap->sta_table_lock);
1732 sta = ap_get_sta(local->ap, hdr->addr2);
1733 if (sta != NULL) {
1734 if ((sta->flags & WLAN_STA_ASSOC) && !sta->ap)
1735 hostap_event_expired_sta(local->dev, sta);
1736 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
1737 }
1738 spin_unlock_bh(&local->ap->sta_table_lock);
1739 if (sta == NULL) {
Johannes Berge1749612008-10-27 15:59:26 -07001740 printk("%s: deauthentication from %pM, "
Jouni Malinenff1d2762005-05-12 22:54:16 -04001741 "reason_code=%d, but STA not authenticated\n", dev->name,
Johannes Berge1749612008-10-27 15:59:26 -07001742 hdr->addr2, reason_code);
Jouni Malinenff1d2762005-05-12 22:54:16 -04001743 }
1744}
1745
1746
1747/* Called only as a scheduled task for pending AP frames. */
1748static void handle_disassoc(local_info_t *local, struct sk_buff *skb,
1749 struct hostap_80211_rx_status *rx_stats)
1750{
1751 struct net_device *dev = local->dev;
James Ketrenosd0416742005-09-21 12:23:49 -05001752 struct ieee80211_hdr_4addr *hdr = (struct ieee80211_hdr_4addr *) skb->data;
Jouni Malinenff1d2762005-05-12 22:54:16 -04001753 char *body = skb->data + IEEE80211_MGMT_HDR_LEN;
1754 int len;
Al Viro8a9faf32007-12-21 03:30:16 -05001755 u16 reason_code;
1756 __le16 *pos;
Jouni Malinenff1d2762005-05-12 22:54:16 -04001757 struct sta_info *sta = NULL;
1758
1759 len = skb->len - IEEE80211_MGMT_HDR_LEN;
1760
1761 if (len < 2) {
1762 printk("handle_disassoc - too short payload (len=%d)\n", len);
1763 return;
1764 }
1765
Al Viro8a9faf32007-12-21 03:30:16 -05001766 pos = (__le16 *) body;
1767 reason_code = le16_to_cpu(*pos);
Jouni Malinenff1d2762005-05-12 22:54:16 -04001768
Johannes Berge1749612008-10-27 15:59:26 -07001769 PDEBUG(DEBUG_AP, "%s: disassociation: %pM len=%d, "
1770 "reason_code=%d\n", dev->name, hdr->addr2,
David S. Miller21f644f2008-04-08 16:50:44 -07001771 len, reason_code);
Jouni Malinenff1d2762005-05-12 22:54:16 -04001772
1773 spin_lock_bh(&local->ap->sta_table_lock);
1774 sta = ap_get_sta(local->ap, hdr->addr2);
1775 if (sta != NULL) {
1776 if ((sta->flags & WLAN_STA_ASSOC) && !sta->ap)
1777 hostap_event_expired_sta(local->dev, sta);
1778 sta->flags &= ~WLAN_STA_ASSOC;
1779 }
1780 spin_unlock_bh(&local->ap->sta_table_lock);
1781 if (sta == NULL) {
Johannes Berge1749612008-10-27 15:59:26 -07001782 printk("%s: disassociation from %pM, "
Jouni Malinenff1d2762005-05-12 22:54:16 -04001783 "reason_code=%d, but STA not authenticated\n",
Johannes Berge1749612008-10-27 15:59:26 -07001784 dev->name, hdr->addr2, reason_code);
Jouni Malinenff1d2762005-05-12 22:54:16 -04001785 }
1786}
1787
1788
1789/* Called only as a scheduled task for pending AP frames. */
1790static void ap_handle_data_nullfunc(local_info_t *local,
James Ketrenosd0416742005-09-21 12:23:49 -05001791 struct ieee80211_hdr_4addr *hdr)
Jouni Malinenff1d2762005-05-12 22:54:16 -04001792{
1793 struct net_device *dev = local->dev;
1794
1795 /* some STA f/w's seem to require control::ACK frame for
1796 * data::nullfunc, but at least Prism2 station f/w version 0.8.0 does
1797 * not send this..
1798 * send control::ACK for the data::nullfunc */
1799
1800 printk(KERN_DEBUG "Sending control::ACK for data::nullfunc\n");
Jouni Malinen4339d322005-08-14 19:08:44 -07001801 prism2_send_mgmt(dev, IEEE80211_FTYPE_CTL | IEEE80211_STYPE_ACK,
Jouni Malinenff1d2762005-05-12 22:54:16 -04001802 NULL, 0, hdr->addr2, 0);
1803}
1804
1805
1806/* Called only as a scheduled task for pending AP frames. */
1807static void ap_handle_dropped_data(local_info_t *local,
James Ketrenosd0416742005-09-21 12:23:49 -05001808 struct ieee80211_hdr_4addr *hdr)
Jouni Malinenff1d2762005-05-12 22:54:16 -04001809{
1810 struct net_device *dev = local->dev;
1811 struct sta_info *sta;
Al Viro8a9faf32007-12-21 03:30:16 -05001812 __le16 reason;
Jouni Malinenff1d2762005-05-12 22:54:16 -04001813
1814 spin_lock_bh(&local->ap->sta_table_lock);
1815 sta = ap_get_sta(local->ap, hdr->addr2);
1816 if (sta)
1817 atomic_inc(&sta->users);
1818 spin_unlock_bh(&local->ap->sta_table_lock);
1819
1820 if (sta != NULL && (sta->flags & WLAN_STA_ASSOC)) {
1821 PDEBUG(DEBUG_AP, "ap_handle_dropped_data: STA is now okay?\n");
1822 atomic_dec(&sta->users);
1823 return;
1824 }
1825
Al Viro8a9faf32007-12-21 03:30:16 -05001826 reason = cpu_to_le16(WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
Jouni Malinen4339d322005-08-14 19:08:44 -07001827 prism2_send_mgmt(dev, IEEE80211_FTYPE_MGMT |
Jouni Malinenff1d2762005-05-12 22:54:16 -04001828 ((sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) ?
Jouni Malinen4339d322005-08-14 19:08:44 -07001829 IEEE80211_STYPE_DEAUTH : IEEE80211_STYPE_DISASSOC),
Jouni Malinenff1d2762005-05-12 22:54:16 -04001830 (char *) &reason, sizeof(reason), hdr->addr2, 0);
1831
1832 if (sta)
1833 atomic_dec(&sta->users);
1834}
1835
1836#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
1837
1838
1839/* Called only as a scheduled task for pending AP frames. */
1840static void pspoll_send_buffered(local_info_t *local, struct sta_info *sta,
1841 struct sk_buff *skb)
1842{
Jouni Malinen5bee7202005-08-14 19:08:39 -07001843 struct hostap_skb_tx_data *meta;
1844
Jouni Malinenff1d2762005-05-12 22:54:16 -04001845 if (!(sta->flags & WLAN_STA_PS)) {
1846 /* Station has moved to non-PS mode, so send all buffered
1847 * frames using normal device queue. */
1848 dev_queue_xmit(skb);
1849 return;
1850 }
1851
1852 /* add a flag for hostap_handle_sta_tx() to know that this skb should
1853 * be passed through even though STA is using PS */
Jouni Malinen5bee7202005-08-14 19:08:39 -07001854 meta = (struct hostap_skb_tx_data *) skb->cb;
1855 meta->flags |= HOSTAP_TX_FLAGS_BUFFERED_FRAME;
Jouni Malinenff1d2762005-05-12 22:54:16 -04001856 if (!skb_queue_empty(&sta->tx_buf)) {
1857 /* indicate to STA that more frames follow */
Jouni Malinen5bee7202005-08-14 19:08:39 -07001858 meta->flags |= HOSTAP_TX_FLAGS_ADD_MOREDATA;
Jouni Malinenff1d2762005-05-12 22:54:16 -04001859 }
1860 dev_queue_xmit(skb);
1861}
1862
1863
1864/* Called only as a scheduled task for pending AP frames. */
1865static void handle_pspoll(local_info_t *local,
James Ketrenosd0416742005-09-21 12:23:49 -05001866 struct ieee80211_hdr_4addr *hdr,
Jouni Malinenff1d2762005-05-12 22:54:16 -04001867 struct hostap_80211_rx_status *rx_stats)
1868{
1869 struct net_device *dev = local->dev;
1870 struct sta_info *sta;
1871 u16 aid;
1872 struct sk_buff *skb;
1873
Johannes Berge1749612008-10-27 15:59:26 -07001874 PDEBUG(DEBUG_PS2, "handle_pspoll: BSSID=%pM, TA=%pM PWRMGT=%d\n",
1875 hdr->addr1, hdr->addr2,
Jouni Malinenb2f4a2e2005-08-14 21:00:01 -07001876 !!(le16_to_cpu(hdr->frame_ctl) & IEEE80211_FCTL_PM));
Jouni Malinenff1d2762005-05-12 22:54:16 -04001877
1878 if (memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN)) {
Johannes Berge1749612008-10-27 15:59:26 -07001879 PDEBUG(DEBUG_AP,
1880 "handle_pspoll - addr1(BSSID)=%pM not own MAC\n",
1881 hdr->addr1);
Jouni Malinenff1d2762005-05-12 22:54:16 -04001882 return;
1883 }
1884
Al Viro8a9faf32007-12-21 03:30:16 -05001885 aid = le16_to_cpu(hdr->duration_id);
Jouni Malinenff1d2762005-05-12 22:54:16 -04001886 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14))) {
1887 PDEBUG(DEBUG_PS, " PSPOLL and AID[15:14] not set\n");
1888 return;
1889 }
Pavel Roskin1bcca3c2008-06-27 16:19:58 -04001890 aid &= ~(BIT(15) | BIT(14));
Jouni Malinenff1d2762005-05-12 22:54:16 -04001891 if (aid == 0 || aid > MAX_AID_TABLE_SIZE) {
1892 PDEBUG(DEBUG_PS, " invalid aid=%d\n", aid);
1893 return;
1894 }
1895 PDEBUG(DEBUG_PS2, " aid=%d\n", aid);
1896
1897 spin_lock_bh(&local->ap->sta_table_lock);
1898 sta = ap_get_sta(local->ap, hdr->addr2);
1899 if (sta)
1900 atomic_inc(&sta->users);
1901 spin_unlock_bh(&local->ap->sta_table_lock);
1902
1903 if (sta == NULL) {
1904 PDEBUG(DEBUG_PS, " STA not found\n");
1905 return;
1906 }
1907 if (sta->aid != aid) {
1908 PDEBUG(DEBUG_PS, " received aid=%i does not match with "
1909 "assoc.aid=%d\n", aid, sta->aid);
1910 return;
1911 }
1912
1913 /* FIX: todo:
1914 * - add timeout for buffering (clear aid in TIM vector if buffer timed
1915 * out (expiry time must be longer than ListenInterval for
1916 * the corresponding STA; "8802-11: 11.2.1.9 AP aging function"
1917 * - what to do, if buffered, pspolled, and sent frame is not ACKed by
1918 * sta; store buffer for later use and leave TIM aid bit set? use
1919 * TX event to check whether frame was ACKed?
1920 */
1921
1922 while ((skb = skb_dequeue(&sta->tx_buf)) != NULL) {
1923 /* send buffered frame .. */
1924 PDEBUG(DEBUG_PS2, "Sending buffered frame to STA after PS POLL"
1925 " (buffer_count=%d)\n", skb_queue_len(&sta->tx_buf));
1926
1927 pspoll_send_buffered(local, sta, skb);
1928
1929 if (sta->flags & WLAN_STA_PS) {
1930 /* send only one buffered packet per PS Poll */
1931 /* FIX: should ignore further PS Polls until the
1932 * buffered packet that was just sent is acknowledged
1933 * (Tx or TxExc event) */
1934 break;
1935 }
1936 }
1937
1938 if (skb_queue_empty(&sta->tx_buf)) {
1939 /* try to clear aid from TIM */
1940 if (!(sta->flags & WLAN_STA_TIM))
1941 PDEBUG(DEBUG_PS2, "Re-unsetting TIM for aid %d\n",
1942 aid);
1943 hostap_set_tim(local, aid, 0);
1944 sta->flags &= ~WLAN_STA_TIM;
1945 }
1946
1947 atomic_dec(&sta->users);
1948}
1949
1950
1951#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
1952
David Howellsc4028952006-11-22 14:57:56 +00001953static void handle_wds_oper_queue(struct work_struct *work)
Jouni Malinenff1d2762005-05-12 22:54:16 -04001954{
David Howellsc4028952006-11-22 14:57:56 +00001955 struct ap_data *ap = container_of(work, struct ap_data,
1956 wds_oper_queue);
1957 local_info_t *local = ap->local;
Jouni Malinenff1d2762005-05-12 22:54:16 -04001958 struct wds_oper_data *entry, *prev;
1959
1960 spin_lock_bh(&local->lock);
1961 entry = local->ap->wds_oper_entries;
1962 local->ap->wds_oper_entries = NULL;
1963 spin_unlock_bh(&local->lock);
1964
1965 while (entry) {
1966 PDEBUG(DEBUG_AP, "%s: %s automatic WDS connection "
Johannes Berge1749612008-10-27 15:59:26 -07001967 "to AP %pM\n",
Jouni Malinenff1d2762005-05-12 22:54:16 -04001968 local->dev->name,
1969 entry->type == WDS_ADD ? "adding" : "removing",
Johannes Berge1749612008-10-27 15:59:26 -07001970 entry->addr);
Jouni Malinenff1d2762005-05-12 22:54:16 -04001971 if (entry->type == WDS_ADD)
1972 prism2_wds_add(local, entry->addr, 0);
1973 else if (entry->type == WDS_DEL)
1974 prism2_wds_del(local, entry->addr, 0, 1);
1975
1976 prev = entry;
1977 entry = entry->next;
1978 kfree(prev);
1979 }
1980}
1981
1982
1983/* Called only as a scheduled task for pending AP frames. */
1984static void handle_beacon(local_info_t *local, struct sk_buff *skb,
1985 struct hostap_80211_rx_status *rx_stats)
1986{
James Ketrenosd0416742005-09-21 12:23:49 -05001987 struct ieee80211_hdr_4addr *hdr = (struct ieee80211_hdr_4addr *) skb->data;
Jouni Malinenff1d2762005-05-12 22:54:16 -04001988 char *body = skb->data + IEEE80211_MGMT_HDR_LEN;
1989 int len, left;
Al Viro8a9faf32007-12-21 03:30:16 -05001990 u16 beacon_int, capability;
1991 __le16 *pos;
Jouni Malinenff1d2762005-05-12 22:54:16 -04001992 char *ssid = NULL;
1993 unsigned char *supp_rates = NULL;
1994 int ssid_len = 0, supp_rates_len = 0;
1995 struct sta_info *sta = NULL;
1996 int new_sta = 0, channel = -1;
1997
1998 len = skb->len - IEEE80211_MGMT_HDR_LEN;
1999
2000 if (len < 8 + 2 + 2) {
2001 printk(KERN_DEBUG "handle_beacon - too short payload "
2002 "(len=%d)\n", len);
2003 return;
2004 }
2005
Al Viro8a9faf32007-12-21 03:30:16 -05002006 pos = (__le16 *) body;
Jouni Malinenff1d2762005-05-12 22:54:16 -04002007 left = len;
2008
2009 /* Timestamp (8 octets) */
2010 pos += 4; left -= 8;
2011 /* Beacon interval (2 octets) */
Al Viro8a9faf32007-12-21 03:30:16 -05002012 beacon_int = le16_to_cpu(*pos);
Jouni Malinenff1d2762005-05-12 22:54:16 -04002013 pos++; left -= 2;
2014 /* Capability information (2 octets) */
Al Viro8a9faf32007-12-21 03:30:16 -05002015 capability = le16_to_cpu(*pos);
Jouni Malinenff1d2762005-05-12 22:54:16 -04002016 pos++; left -= 2;
2017
2018 if (local->ap->ap_policy != AP_OTHER_AP_EVEN_IBSS &&
2019 capability & WLAN_CAPABILITY_IBSS)
2020 return;
2021
2022 if (left >= 2) {
2023 unsigned int ileft;
2024 unsigned char *u = (unsigned char *) pos;
2025
2026 if (*u == WLAN_EID_SSID) {
2027 u++; left--;
2028 ileft = *u;
2029 u++; left--;
2030
2031 if (ileft > left || ileft > MAX_SSID_LEN) {
2032 PDEBUG(DEBUG_AP, "SSID: overflow\n");
2033 return;
2034 }
2035
2036 if (local->ap->ap_policy == AP_OTHER_AP_SAME_SSID &&
2037 (ileft != strlen(local->essid) ||
2038 memcmp(local->essid, u, ileft) != 0)) {
2039 /* not our SSID */
2040 return;
2041 }
2042
2043 ssid = u;
2044 ssid_len = ileft;
2045
2046 u += ileft;
2047 left -= ileft;
2048 }
2049
2050 if (*u == WLAN_EID_SUPP_RATES) {
2051 u++; left--;
2052 ileft = *u;
2053 u++; left--;
Jeff Garzik74fae822005-07-31 13:08:32 -04002054
Jouni Malinenff1d2762005-05-12 22:54:16 -04002055 if (ileft > left || ileft == 0 || ileft > 8) {
2056 PDEBUG(DEBUG_AP, " - SUPP_RATES len error\n");
2057 return;
2058 }
2059
2060 supp_rates = u;
2061 supp_rates_len = ileft;
2062
2063 u += ileft;
2064 left -= ileft;
2065 }
2066
2067 if (*u == WLAN_EID_DS_PARAMS) {
2068 u++; left--;
2069 ileft = *u;
2070 u++; left--;
Jeff Garzik74fae822005-07-31 13:08:32 -04002071
Jouni Malinenff1d2762005-05-12 22:54:16 -04002072 if (ileft > left || ileft != 1) {
2073 PDEBUG(DEBUG_AP, " - DS_PARAMS len error\n");
2074 return;
2075 }
2076
2077 channel = *u;
2078
2079 u += ileft;
2080 left -= ileft;
2081 }
2082 }
2083
2084 spin_lock_bh(&local->ap->sta_table_lock);
2085 sta = ap_get_sta(local->ap, hdr->addr2);
2086 if (sta != NULL)
2087 atomic_inc(&sta->users);
2088 spin_unlock_bh(&local->ap->sta_table_lock);
2089
2090 if (sta == NULL) {
2091 /* add new AP */
2092 new_sta = 1;
2093 sta = ap_add_sta(local->ap, hdr->addr2);
2094 if (sta == NULL) {
2095 printk(KERN_INFO "prism2: kmalloc failed for AP "
2096 "data structure\n");
2097 return;
2098 }
2099 hostap_event_new_sta(local->dev, sta);
2100
2101 /* mark APs authentication and associated for pseudo ad-hoc
2102 * style communication */
2103 sta->flags = WLAN_STA_AUTH | WLAN_STA_ASSOC;
2104
2105 if (local->ap->autom_ap_wds) {
2106 hostap_wds_link_oper(local, sta->addr, WDS_ADD);
2107 }
2108 }
2109
2110 sta->ap = 1;
2111 if (ssid) {
2112 sta->u.ap.ssid_len = ssid_len;
2113 memcpy(sta->u.ap.ssid, ssid, ssid_len);
2114 sta->u.ap.ssid[ssid_len] = '\0';
2115 } else {
2116 sta->u.ap.ssid_len = 0;
2117 sta->u.ap.ssid[0] = '\0';
2118 }
2119 sta->u.ap.channel = channel;
2120 sta->rx_packets++;
2121 sta->rx_bytes += len;
2122 sta->u.ap.last_beacon = sta->last_rx = jiffies;
2123 sta->capability = capability;
2124 sta->listen_interval = beacon_int;
2125
2126 atomic_dec(&sta->users);
2127
2128 if (new_sta) {
2129 memset(sta->supported_rates, 0, sizeof(sta->supported_rates));
2130 memcpy(sta->supported_rates, supp_rates, supp_rates_len);
2131 prism2_check_tx_rates(sta);
2132 }
2133}
2134
2135#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
2136
2137
2138/* Called only as a tasklet. */
2139static void handle_ap_item(local_info_t *local, struct sk_buff *skb,
2140 struct hostap_80211_rx_status *rx_stats)
2141{
2142#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
2143 struct net_device *dev = local->dev;
2144#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
2145 u16 fc, type, stype;
James Ketrenosd0416742005-09-21 12:23:49 -05002146 struct ieee80211_hdr_4addr *hdr;
Jouni Malinenff1d2762005-05-12 22:54:16 -04002147
2148 /* FIX: should give skb->len to handler functions and check that the
2149 * buffer is long enough */
James Ketrenosd0416742005-09-21 12:23:49 -05002150 hdr = (struct ieee80211_hdr_4addr *) skb->data;
Jouni Malinenc0f72ca2005-08-14 19:08:43 -07002151 fc = le16_to_cpu(hdr->frame_ctl);
Jouni Malinen4339d322005-08-14 19:08:44 -07002152 type = WLAN_FC_GET_TYPE(fc);
2153 stype = WLAN_FC_GET_STYPE(fc);
Jouni Malinenff1d2762005-05-12 22:54:16 -04002154
2155#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
Jouni Malinen4339d322005-08-14 19:08:44 -07002156 if (!local->hostapd && type == IEEE80211_FTYPE_DATA) {
Jouni Malinenff1d2762005-05-12 22:54:16 -04002157 PDEBUG(DEBUG_AP, "handle_ap_item - data frame\n");
2158
Jouni Malinenb2f4a2e2005-08-14 21:00:01 -07002159 if (!(fc & IEEE80211_FCTL_TODS) ||
2160 (fc & IEEE80211_FCTL_FROMDS)) {
Jouni Malinen4339d322005-08-14 19:08:44 -07002161 if (stype == IEEE80211_STYPE_NULLFUNC) {
Jouni Malinenff1d2762005-05-12 22:54:16 -04002162 /* no ToDS nullfunc seems to be used to check
2163 * AP association; so send reject message to
2164 * speed up re-association */
2165 ap_handle_dropped_data(local, hdr);
2166 goto done;
2167 }
2168 PDEBUG(DEBUG_AP, " not ToDS frame (fc=0x%04x)\n",
2169 fc);
2170 goto done;
2171 }
2172
2173 if (memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN)) {
Johannes Berge1749612008-10-27 15:59:26 -07002174 PDEBUG(DEBUG_AP, "handle_ap_item - addr1(BSSID)=%pM"
2175 " not own MAC\n", hdr->addr1);
Jouni Malinenff1d2762005-05-12 22:54:16 -04002176 goto done;
2177 }
2178
Jouni Malinen4339d322005-08-14 19:08:44 -07002179 if (local->ap->nullfunc_ack &&
2180 stype == IEEE80211_STYPE_NULLFUNC)
Jouni Malinenff1d2762005-05-12 22:54:16 -04002181 ap_handle_data_nullfunc(local, hdr);
2182 else
2183 ap_handle_dropped_data(local, hdr);
2184 goto done;
2185 }
2186
Jouni Malinen4339d322005-08-14 19:08:44 -07002187 if (type == IEEE80211_FTYPE_MGMT && stype == IEEE80211_STYPE_BEACON) {
Jouni Malinenff1d2762005-05-12 22:54:16 -04002188 handle_beacon(local, skb, rx_stats);
2189 goto done;
2190 }
2191#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
2192
Jouni Malinen4339d322005-08-14 19:08:44 -07002193 if (type == IEEE80211_FTYPE_CTL && stype == IEEE80211_STYPE_PSPOLL) {
Jouni Malinenff1d2762005-05-12 22:54:16 -04002194 handle_pspoll(local, hdr, rx_stats);
2195 goto done;
2196 }
2197
2198 if (local->hostapd) {
2199 PDEBUG(DEBUG_AP, "Unknown frame in AP queue: type=0x%02x "
2200 "subtype=0x%02x\n", type, stype);
2201 goto done;
2202 }
2203
2204#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
Jouni Malinen4339d322005-08-14 19:08:44 -07002205 if (type != IEEE80211_FTYPE_MGMT) {
Jouni Malinenff1d2762005-05-12 22:54:16 -04002206 PDEBUG(DEBUG_AP, "handle_ap_item - not a management frame?\n");
2207 goto done;
2208 }
2209
2210 if (memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN)) {
Johannes Berge1749612008-10-27 15:59:26 -07002211 PDEBUG(DEBUG_AP, "handle_ap_item - addr1(DA)=%pM"
2212 " not own MAC\n", hdr->addr1);
Jouni Malinenff1d2762005-05-12 22:54:16 -04002213 goto done;
2214 }
2215
2216 if (memcmp(hdr->addr3, dev->dev_addr, ETH_ALEN)) {
Johannes Berge1749612008-10-27 15:59:26 -07002217 PDEBUG(DEBUG_AP, "handle_ap_item - addr3(BSSID)=%pM"
2218 " not own MAC\n", hdr->addr3);
Jouni Malinenff1d2762005-05-12 22:54:16 -04002219 goto done;
2220 }
2221
2222 switch (stype) {
Jouni Malinen4339d322005-08-14 19:08:44 -07002223 case IEEE80211_STYPE_ASSOC_REQ:
Jouni Malinenff1d2762005-05-12 22:54:16 -04002224 handle_assoc(local, skb, rx_stats, 0);
2225 break;
Jouni Malinen4339d322005-08-14 19:08:44 -07002226 case IEEE80211_STYPE_ASSOC_RESP:
Jouni Malinenff1d2762005-05-12 22:54:16 -04002227 PDEBUG(DEBUG_AP, "==> ASSOC RESP (ignored)\n");
2228 break;
Jouni Malinen4339d322005-08-14 19:08:44 -07002229 case IEEE80211_STYPE_REASSOC_REQ:
Jouni Malinenff1d2762005-05-12 22:54:16 -04002230 handle_assoc(local, skb, rx_stats, 1);
2231 break;
Jouni Malinen4339d322005-08-14 19:08:44 -07002232 case IEEE80211_STYPE_REASSOC_RESP:
Jouni Malinenff1d2762005-05-12 22:54:16 -04002233 PDEBUG(DEBUG_AP, "==> REASSOC RESP (ignored)\n");
2234 break;
Jouni Malinen4339d322005-08-14 19:08:44 -07002235 case IEEE80211_STYPE_ATIM:
Jouni Malinenff1d2762005-05-12 22:54:16 -04002236 PDEBUG(DEBUG_AP, "==> ATIM (ignored)\n");
2237 break;
Jouni Malinen4339d322005-08-14 19:08:44 -07002238 case IEEE80211_STYPE_DISASSOC:
Jouni Malinenff1d2762005-05-12 22:54:16 -04002239 handle_disassoc(local, skb, rx_stats);
2240 break;
Jouni Malinen4339d322005-08-14 19:08:44 -07002241 case IEEE80211_STYPE_AUTH:
Jouni Malinenff1d2762005-05-12 22:54:16 -04002242 handle_authen(local, skb, rx_stats);
2243 break;
Jouni Malinen4339d322005-08-14 19:08:44 -07002244 case IEEE80211_STYPE_DEAUTH:
Jouni Malinenff1d2762005-05-12 22:54:16 -04002245 handle_deauth(local, skb, rx_stats);
2246 break;
2247 default:
Jouni Malinen4339d322005-08-14 19:08:44 -07002248 PDEBUG(DEBUG_AP, "Unknown mgmt frame subtype 0x%02x\n",
2249 stype >> 4);
Jouni Malinenff1d2762005-05-12 22:54:16 -04002250 break;
2251 }
2252#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
2253
2254 done:
2255 dev_kfree_skb(skb);
2256}
2257
2258
2259/* Called only as a tasklet (software IRQ) */
2260void hostap_rx(struct net_device *dev, struct sk_buff *skb,
2261 struct hostap_80211_rx_status *rx_stats)
2262{
2263 struct hostap_interface *iface;
2264 local_info_t *local;
2265 u16 fc;
James Ketrenosd0416742005-09-21 12:23:49 -05002266 struct ieee80211_hdr_4addr *hdr;
Jouni Malinenff1d2762005-05-12 22:54:16 -04002267
2268 iface = netdev_priv(dev);
2269 local = iface->local;
2270
2271 if (skb->len < 16)
2272 goto drop;
2273
2274 local->stats.rx_packets++;
2275
James Ketrenosd0416742005-09-21 12:23:49 -05002276 hdr = (struct ieee80211_hdr_4addr *) skb->data;
Jouni Malinenc0f72ca2005-08-14 19:08:43 -07002277 fc = le16_to_cpu(hdr->frame_ctl);
Jouni Malinenff1d2762005-05-12 22:54:16 -04002278
2279 if (local->ap->ap_policy == AP_OTHER_AP_SKIP_ALL &&
Jouni Malinen4339d322005-08-14 19:08:44 -07002280 WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_MGMT &&
2281 WLAN_FC_GET_STYPE(fc) == IEEE80211_STYPE_BEACON)
Jouni Malinenff1d2762005-05-12 22:54:16 -04002282 goto drop;
2283
2284 skb->protocol = __constant_htons(ETH_P_HOSTAP);
2285 handle_ap_item(local, skb, rx_stats);
2286 return;
2287
2288 drop:
2289 dev_kfree_skb(skb);
2290}
2291
2292
2293/* Called only as a tasklet (software IRQ) */
2294static void schedule_packet_send(local_info_t *local, struct sta_info *sta)
2295{
2296 struct sk_buff *skb;
James Ketrenosd0416742005-09-21 12:23:49 -05002297 struct ieee80211_hdr_4addr *hdr;
Jouni Malinenff1d2762005-05-12 22:54:16 -04002298 struct hostap_80211_rx_status rx_stats;
2299
2300 if (skb_queue_empty(&sta->tx_buf))
2301 return;
2302
2303 skb = dev_alloc_skb(16);
2304 if (skb == NULL) {
2305 printk(KERN_DEBUG "%s: schedule_packet_send: skb alloc "
2306 "failed\n", local->dev->name);
2307 return;
2308 }
2309
James Ketrenosd0416742005-09-21 12:23:49 -05002310 hdr = (struct ieee80211_hdr_4addr *) skb_put(skb, 16);
Jouni Malinenff1d2762005-05-12 22:54:16 -04002311
2312 /* Generate a fake pspoll frame to start packet delivery */
Jouni Malinenc0f72ca2005-08-14 19:08:43 -07002313 hdr->frame_ctl = __constant_cpu_to_le16(
Jouni Malinen4339d322005-08-14 19:08:44 -07002314 IEEE80211_FTYPE_CTL | IEEE80211_STYPE_PSPOLL);
Jouni Malinenff1d2762005-05-12 22:54:16 -04002315 memcpy(hdr->addr1, local->dev->dev_addr, ETH_ALEN);
2316 memcpy(hdr->addr2, sta->addr, ETH_ALEN);
2317 hdr->duration_id = cpu_to_le16(sta->aid | BIT(15) | BIT(14));
2318
Johannes Berge1749612008-10-27 15:59:26 -07002319 PDEBUG(DEBUG_PS2,
2320 "%s: Scheduling buffered packet delivery for STA %pM\n",
2321 local->dev->name, sta->addr);
Jouni Malinenff1d2762005-05-12 22:54:16 -04002322
2323 skb->dev = local->dev;
2324
2325 memset(&rx_stats, 0, sizeof(rx_stats));
2326 hostap_rx(local->dev, skb, &rx_stats);
2327}
2328
2329
Adrian Bunk5fad5a22006-01-14 03:09:34 +01002330int prism2_ap_get_sta_qual(local_info_t *local, struct sockaddr addr[],
2331 struct iw_quality qual[], int buf_size,
2332 int aplist)
Jouni Malinenff1d2762005-05-12 22:54:16 -04002333{
2334 struct ap_data *ap = local->ap;
2335 struct list_head *ptr;
2336 int count = 0;
2337
2338 spin_lock_bh(&ap->sta_table_lock);
2339
2340 for (ptr = ap->sta_list.next; ptr != NULL && ptr != &ap->sta_list;
2341 ptr = ptr->next) {
2342 struct sta_info *sta = (struct sta_info *) ptr;
2343
2344 if (aplist && !sta->ap)
2345 continue;
2346 addr[count].sa_family = ARPHRD_ETHER;
2347 memcpy(addr[count].sa_data, sta->addr, ETH_ALEN);
2348 if (sta->last_rx_silence == 0)
2349 qual[count].qual = sta->last_rx_signal < 27 ?
2350 0 : (sta->last_rx_signal - 27) * 92 / 127;
2351 else
2352 qual[count].qual = sta->last_rx_signal -
2353 sta->last_rx_silence - 35;
2354 qual[count].level = HFA384X_LEVEL_TO_dBm(sta->last_rx_signal);
2355 qual[count].noise = HFA384X_LEVEL_TO_dBm(sta->last_rx_silence);
2356 qual[count].updated = sta->last_rx_updated;
2357
Jean Tourrilhesc28df16e2005-09-23 21:58:59 -07002358 sta->last_rx_updated = IW_QUAL_DBM;
Jouni Malinenff1d2762005-05-12 22:54:16 -04002359
2360 count++;
2361 if (count >= buf_size)
2362 break;
2363 }
2364 spin_unlock_bh(&ap->sta_table_lock);
2365
2366 return count;
2367}
2368
2369
2370/* Translate our list of Access Points & Stations to a card independant
2371 * format that the Wireless Tools will understand - Jean II */
David S. Millerccc58052008-06-16 18:50:49 -07002372int prism2_ap_translate_scan(struct net_device *dev,
2373 struct iw_request_info *info, char *buffer)
Jouni Malinenff1d2762005-05-12 22:54:16 -04002374{
2375 struct hostap_interface *iface;
2376 local_info_t *local;
2377 struct ap_data *ap;
2378 struct list_head *ptr;
2379 struct iw_event iwe;
2380 char *current_ev = buffer;
2381 char *end_buf = buffer + IW_SCAN_MAX_DATA;
2382#if !defined(PRISM2_NO_KERNEL_IEEE80211_MGMT)
2383 char buf[64];
2384#endif
2385
2386 iface = netdev_priv(dev);
2387 local = iface->local;
2388 ap = local->ap;
2389
2390 spin_lock_bh(&ap->sta_table_lock);
2391
2392 for (ptr = ap->sta_list.next; ptr != NULL && ptr != &ap->sta_list;
2393 ptr = ptr->next) {
2394 struct sta_info *sta = (struct sta_info *) ptr;
2395
2396 /* First entry *MUST* be the AP MAC address */
2397 memset(&iwe, 0, sizeof(iwe));
2398 iwe.cmd = SIOCGIWAP;
2399 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
2400 memcpy(iwe.u.ap_addr.sa_data, sta->addr, ETH_ALEN);
2401 iwe.len = IW_EV_ADDR_LEN;
David S. Millerccc58052008-06-16 18:50:49 -07002402 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
2403 &iwe, IW_EV_ADDR_LEN);
Jouni Malinenff1d2762005-05-12 22:54:16 -04002404
2405 /* Use the mode to indicate if it's a station or
2406 * an Access Point */
2407 memset(&iwe, 0, sizeof(iwe));
2408 iwe.cmd = SIOCGIWMODE;
2409 if (sta->ap)
2410 iwe.u.mode = IW_MODE_MASTER;
2411 else
2412 iwe.u.mode = IW_MODE_INFRA;
2413 iwe.len = IW_EV_UINT_LEN;
David S. Millerccc58052008-06-16 18:50:49 -07002414 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
2415 &iwe, IW_EV_UINT_LEN);
Jouni Malinenff1d2762005-05-12 22:54:16 -04002416
2417 /* Some quality */
2418 memset(&iwe, 0, sizeof(iwe));
2419 iwe.cmd = IWEVQUAL;
2420 if (sta->last_rx_silence == 0)
2421 iwe.u.qual.qual = sta->last_rx_signal < 27 ?
2422 0 : (sta->last_rx_signal - 27) * 92 / 127;
2423 else
2424 iwe.u.qual.qual = sta->last_rx_signal -
2425 sta->last_rx_silence - 35;
2426 iwe.u.qual.level = HFA384X_LEVEL_TO_dBm(sta->last_rx_signal);
2427 iwe.u.qual.noise = HFA384X_LEVEL_TO_dBm(sta->last_rx_silence);
2428 iwe.u.qual.updated = sta->last_rx_updated;
2429 iwe.len = IW_EV_QUAL_LEN;
David S. Millerccc58052008-06-16 18:50:49 -07002430 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
2431 &iwe, IW_EV_QUAL_LEN);
Jouni Malinenff1d2762005-05-12 22:54:16 -04002432
2433#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
2434 if (sta->ap) {
2435 memset(&iwe, 0, sizeof(iwe));
2436 iwe.cmd = SIOCGIWESSID;
2437 iwe.u.data.length = sta->u.ap.ssid_len;
2438 iwe.u.data.flags = 1;
David S. Millerccc58052008-06-16 18:50:49 -07002439 current_ev = iwe_stream_add_point(info, current_ev,
2440 end_buf, &iwe,
Jouni Malinenff1d2762005-05-12 22:54:16 -04002441 sta->u.ap.ssid);
2442
2443 memset(&iwe, 0, sizeof(iwe));
2444 iwe.cmd = SIOCGIWENCODE;
2445 if (sta->capability & WLAN_CAPABILITY_PRIVACY)
2446 iwe.u.data.flags =
2447 IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
2448 else
2449 iwe.u.data.flags = IW_ENCODE_DISABLED;
David S. Millerccc58052008-06-16 18:50:49 -07002450 current_ev = iwe_stream_add_point(info, current_ev,
2451 end_buf, &iwe,
2452 sta->u.ap.ssid);
Jouni Malinenff1d2762005-05-12 22:54:16 -04002453
2454 if (sta->u.ap.channel > 0 &&
2455 sta->u.ap.channel <= FREQ_COUNT) {
2456 memset(&iwe, 0, sizeof(iwe));
2457 iwe.cmd = SIOCGIWFREQ;
2458 iwe.u.freq.m = freq_list[sta->u.ap.channel - 1]
2459 * 100000;
2460 iwe.u.freq.e = 1;
2461 current_ev = iwe_stream_add_event(
David S. Millerccc58052008-06-16 18:50:49 -07002462 info, current_ev, end_buf, &iwe,
Jouni Malinenff1d2762005-05-12 22:54:16 -04002463 IW_EV_FREQ_LEN);
2464 }
2465
2466 memset(&iwe, 0, sizeof(iwe));
2467 iwe.cmd = IWEVCUSTOM;
2468 sprintf(buf, "beacon_interval=%d",
2469 sta->listen_interval);
2470 iwe.u.data.length = strlen(buf);
David S. Millerccc58052008-06-16 18:50:49 -07002471 current_ev = iwe_stream_add_point(info, current_ev,
2472 end_buf, &iwe, buf);
Jouni Malinenff1d2762005-05-12 22:54:16 -04002473 }
2474#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
2475
Jean Tourrilhesc28df16e2005-09-23 21:58:59 -07002476 sta->last_rx_updated = IW_QUAL_DBM;
Jouni Malinenff1d2762005-05-12 22:54:16 -04002477
2478 /* To be continued, we should make good use of IWEVCUSTOM */
2479 }
2480
2481 spin_unlock_bh(&ap->sta_table_lock);
2482
2483 return current_ev - buffer;
2484}
2485
2486
2487static int prism2_hostapd_add_sta(struct ap_data *ap,
2488 struct prism2_hostapd_param *param)
2489{
2490 struct sta_info *sta;
2491
2492 spin_lock_bh(&ap->sta_table_lock);
2493 sta = ap_get_sta(ap, param->sta_addr);
2494 if (sta)
2495 atomic_inc(&sta->users);
2496 spin_unlock_bh(&ap->sta_table_lock);
2497
2498 if (sta == NULL) {
2499 sta = ap_add_sta(ap, param->sta_addr);
2500 if (sta == NULL)
2501 return -1;
2502 }
2503
2504 if (!(sta->flags & WLAN_STA_ASSOC) && !sta->ap && sta->local)
2505 hostap_event_new_sta(sta->local->dev, sta);
2506
2507 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
2508 sta->last_rx = jiffies;
2509 sta->aid = param->u.add_sta.aid;
2510 sta->capability = param->u.add_sta.capability;
2511 sta->tx_supp_rates = param->u.add_sta.tx_supp_rates;
2512 if (sta->tx_supp_rates & WLAN_RATE_1M)
2513 sta->supported_rates[0] = 2;
2514 if (sta->tx_supp_rates & WLAN_RATE_2M)
2515 sta->supported_rates[1] = 4;
2516 if (sta->tx_supp_rates & WLAN_RATE_5M5)
2517 sta->supported_rates[2] = 11;
2518 if (sta->tx_supp_rates & WLAN_RATE_11M)
2519 sta->supported_rates[3] = 22;
2520 prism2_check_tx_rates(sta);
2521 atomic_dec(&sta->users);
2522 return 0;
2523}
2524
2525
2526static int prism2_hostapd_remove_sta(struct ap_data *ap,
2527 struct prism2_hostapd_param *param)
2528{
2529 struct sta_info *sta;
2530
2531 spin_lock_bh(&ap->sta_table_lock);
2532 sta = ap_get_sta(ap, param->sta_addr);
2533 if (sta) {
2534 ap_sta_hash_del(ap, sta);
2535 list_del(&sta->list);
2536 }
2537 spin_unlock_bh(&ap->sta_table_lock);
2538
2539 if (!sta)
2540 return -ENOENT;
2541
2542 if ((sta->flags & WLAN_STA_ASSOC) && !sta->ap && sta->local)
2543 hostap_event_expired_sta(sta->local->dev, sta);
2544 ap_free_sta(ap, sta);
2545
2546 return 0;
2547}
2548
2549
2550static int prism2_hostapd_get_info_sta(struct ap_data *ap,
2551 struct prism2_hostapd_param *param)
2552{
2553 struct sta_info *sta;
2554
2555 spin_lock_bh(&ap->sta_table_lock);
2556 sta = ap_get_sta(ap, param->sta_addr);
2557 if (sta)
2558 atomic_inc(&sta->users);
2559 spin_unlock_bh(&ap->sta_table_lock);
2560
2561 if (!sta)
2562 return -ENOENT;
2563
2564 param->u.get_info_sta.inactive_sec = (jiffies - sta->last_rx) / HZ;
2565
2566 atomic_dec(&sta->users);
2567
2568 return 1;
2569}
2570
2571
2572static int prism2_hostapd_set_flags_sta(struct ap_data *ap,
2573 struct prism2_hostapd_param *param)
2574{
2575 struct sta_info *sta;
2576
2577 spin_lock_bh(&ap->sta_table_lock);
2578 sta = ap_get_sta(ap, param->sta_addr);
2579 if (sta) {
2580 sta->flags |= param->u.set_flags_sta.flags_or;
2581 sta->flags &= param->u.set_flags_sta.flags_and;
2582 }
2583 spin_unlock_bh(&ap->sta_table_lock);
2584
2585 if (!sta)
2586 return -ENOENT;
2587
2588 return 0;
2589}
2590
2591
2592static int prism2_hostapd_sta_clear_stats(struct ap_data *ap,
2593 struct prism2_hostapd_param *param)
2594{
2595 struct sta_info *sta;
2596 int rate;
2597
2598 spin_lock_bh(&ap->sta_table_lock);
2599 sta = ap_get_sta(ap, param->sta_addr);
2600 if (sta) {
2601 sta->rx_packets = sta->tx_packets = 0;
2602 sta->rx_bytes = sta->tx_bytes = 0;
2603 for (rate = 0; rate < WLAN_RATE_COUNT; rate++) {
2604 sta->tx_count[rate] = 0;
2605 sta->rx_count[rate] = 0;
2606 }
2607 }
2608 spin_unlock_bh(&ap->sta_table_lock);
2609
2610 if (!sta)
2611 return -ENOENT;
2612
2613 return 0;
2614}
2615
2616
Adrian Bunk5fad5a22006-01-14 03:09:34 +01002617int prism2_hostapd(struct ap_data *ap, struct prism2_hostapd_param *param)
Jouni Malinenff1d2762005-05-12 22:54:16 -04002618{
2619 switch (param->cmd) {
2620 case PRISM2_HOSTAPD_FLUSH:
2621 ap_control_kickall(ap);
2622 return 0;
2623 case PRISM2_HOSTAPD_ADD_STA:
2624 return prism2_hostapd_add_sta(ap, param);
2625 case PRISM2_HOSTAPD_REMOVE_STA:
2626 return prism2_hostapd_remove_sta(ap, param);
2627 case PRISM2_HOSTAPD_GET_INFO_STA:
2628 return prism2_hostapd_get_info_sta(ap, param);
2629 case PRISM2_HOSTAPD_SET_FLAGS_STA:
2630 return prism2_hostapd_set_flags_sta(ap, param);
2631 case PRISM2_HOSTAPD_STA_CLEAR_STATS:
2632 return prism2_hostapd_sta_clear_stats(ap, param);
2633 default:
2634 printk(KERN_WARNING "prism2_hostapd: unknown cmd=%d\n",
2635 param->cmd);
2636 return -EOPNOTSUPP;
2637 }
2638}
2639
2640
2641/* Update station info for host-based TX rate control and return current
2642 * TX rate */
2643static int ap_update_sta_tx_rate(struct sta_info *sta, struct net_device *dev)
2644{
2645 int ret = sta->tx_rate;
2646 struct hostap_interface *iface;
2647 local_info_t *local;
2648
2649 iface = netdev_priv(dev);
2650 local = iface->local;
2651
2652 sta->tx_count[sta->tx_rate_idx]++;
2653 sta->tx_since_last_failure++;
2654 sta->tx_consecutive_exc = 0;
2655 if (sta->tx_since_last_failure >= WLAN_RATE_UPDATE_COUNT &&
2656 sta->tx_rate_idx < sta->tx_max_rate) {
2657 /* use next higher rate */
2658 int old_rate, new_rate;
2659 old_rate = new_rate = sta->tx_rate_idx;
2660 while (new_rate < sta->tx_max_rate) {
2661 new_rate++;
2662 if (ap_tx_rate_ok(new_rate, sta, local)) {
2663 sta->tx_rate_idx = new_rate;
2664 break;
2665 }
2666 }
2667 if (old_rate != sta->tx_rate_idx) {
2668 switch (sta->tx_rate_idx) {
2669 case 0: sta->tx_rate = 10; break;
2670 case 1: sta->tx_rate = 20; break;
2671 case 2: sta->tx_rate = 55; break;
2672 case 3: sta->tx_rate = 110; break;
2673 default: sta->tx_rate = 0; break;
2674 }
Johannes Berge1749612008-10-27 15:59:26 -07002675 PDEBUG(DEBUG_AP, "%s: STA %pM TX rate raised to %d\n",
2676 dev->name, sta->addr, sta->tx_rate);
Jouni Malinenff1d2762005-05-12 22:54:16 -04002677 }
2678 sta->tx_since_last_failure = 0;
2679 }
2680
2681 return ret;
2682}
2683
2684
2685/* Called only from software IRQ. Called for each TX frame prior possible
2686 * encryption and transmit. */
2687ap_tx_ret hostap_handle_sta_tx(local_info_t *local, struct hostap_tx_data *tx)
2688{
2689 struct sta_info *sta = NULL;
2690 struct sk_buff *skb = tx->skb;
2691 int set_tim, ret;
James Ketrenosd0416742005-09-21 12:23:49 -05002692 struct ieee80211_hdr_4addr *hdr;
Jouni Malinenff1d2762005-05-12 22:54:16 -04002693 struct hostap_skb_tx_data *meta;
2694
2695 meta = (struct hostap_skb_tx_data *) skb->cb;
2696 ret = AP_TX_CONTINUE;
2697 if (local->ap == NULL || skb->len < 10 ||
2698 meta->iface->type == HOSTAP_INTERFACE_STA)
2699 goto out;
2700
James Ketrenosd0416742005-09-21 12:23:49 -05002701 hdr = (struct ieee80211_hdr_4addr *) skb->data;
Jouni Malinenff1d2762005-05-12 22:54:16 -04002702
2703 if (hdr->addr1[0] & 0x01) {
2704 /* broadcast/multicast frame - no AP related processing */
Pavel Roskinb9180992007-05-28 09:38:47 -07002705 if (local->ap->num_sta <= 0)
2706 ret = AP_TX_DROP;
Jouni Malinenff1d2762005-05-12 22:54:16 -04002707 goto out;
2708 }
2709
2710 /* unicast packet - check whether destination STA is associated */
2711 spin_lock(&local->ap->sta_table_lock);
2712 sta = ap_get_sta(local->ap, hdr->addr1);
2713 if (sta)
2714 atomic_inc(&sta->users);
2715 spin_unlock(&local->ap->sta_table_lock);
2716
Jouni Malinen5bee7202005-08-14 19:08:39 -07002717 if (local->iw_mode == IW_MODE_MASTER && sta == NULL &&
2718 !(meta->flags & HOSTAP_TX_FLAGS_WDS) &&
Jouni Malinenff1d2762005-05-12 22:54:16 -04002719 meta->iface->type != HOSTAP_INTERFACE_MASTER &&
2720 meta->iface->type != HOSTAP_INTERFACE_AP) {
2721#if 0
2722 /* This can happen, e.g., when wlan0 is added to a bridge and
2723 * bridging code does not know which port is the correct target
2724 * for a unicast frame. In this case, the packet is send to all
2725 * ports of the bridge. Since this is a valid scenario, do not
2726 * print out any errors here. */
2727 if (net_ratelimit()) {
2728 printk(KERN_DEBUG "AP: drop packet to non-associated "
Johannes Berge1749612008-10-27 15:59:26 -07002729 "STA %pM\n", hdr->addr1);
Jouni Malinenff1d2762005-05-12 22:54:16 -04002730 }
2731#endif
2732 local->ap->tx_drop_nonassoc++;
2733 ret = AP_TX_DROP;
2734 goto out;
2735 }
2736
2737 if (sta == NULL)
2738 goto out;
2739
2740 if (!(sta->flags & WLAN_STA_AUTHORIZED))
2741 ret = AP_TX_CONTINUE_NOT_AUTHORIZED;
2742
2743 /* Set tx_rate if using host-based TX rate control */
2744 if (!local->fw_tx_rate_control)
2745 local->ap->last_tx_rate = meta->rate =
2746 ap_update_sta_tx_rate(sta, local->dev);
2747
2748 if (local->iw_mode != IW_MODE_MASTER)
2749 goto out;
2750
2751 if (!(sta->flags & WLAN_STA_PS))
2752 goto out;
2753
Jouni Malinen5bee7202005-08-14 19:08:39 -07002754 if (meta->flags & HOSTAP_TX_FLAGS_ADD_MOREDATA) {
2755 /* indicate to STA that more frames follow */
Jouni Malinenb2f4a2e2005-08-14 21:00:01 -07002756 hdr->frame_ctl |=
2757 __constant_cpu_to_le16(IEEE80211_FCTL_MOREDATA);
Jouni Malinen5bee7202005-08-14 19:08:39 -07002758 }
Jouni Malinenff1d2762005-05-12 22:54:16 -04002759
Jouni Malinen5bee7202005-08-14 19:08:39 -07002760 if (meta->flags & HOSTAP_TX_FLAGS_BUFFERED_FRAME) {
2761 /* packet was already buffered and now send due to
2762 * PS poll, so do not rebuffer it */
2763 goto out;
Jouni Malinenff1d2762005-05-12 22:54:16 -04002764 }
2765
2766 if (skb_queue_len(&sta->tx_buf) >= STA_MAX_TX_BUFFER) {
Johannes Berge1749612008-10-27 15:59:26 -07002767 PDEBUG(DEBUG_PS, "%s: No more space in STA (%pM)'s"
2768 "PS mode buffer\n",
2769 local->dev->name, sta->addr);
Jouni Malinenff1d2762005-05-12 22:54:16 -04002770 /* Make sure that TIM is set for the station (it might not be
2771 * after AP wlan hw reset). */
2772 /* FIX: should fix hw reset to restore bits based on STA
2773 * buffer state.. */
2774 hostap_set_tim(local, sta->aid, 1);
2775 sta->flags |= WLAN_STA_TIM;
2776 ret = AP_TX_DROP;
2777 goto out;
2778 }
2779
2780 /* STA in PS mode, buffer frame for later delivery */
2781 set_tim = skb_queue_empty(&sta->tx_buf);
2782 skb_queue_tail(&sta->tx_buf, skb);
2783 /* FIX: could save RX time to skb and expire buffered frames after
2784 * some time if STA does not poll for them */
2785
2786 if (set_tim) {
2787 if (sta->flags & WLAN_STA_TIM)
2788 PDEBUG(DEBUG_PS2, "Re-setting TIM for aid %d\n",
2789 sta->aid);
2790 hostap_set_tim(local, sta->aid, 1);
2791 sta->flags |= WLAN_STA_TIM;
2792 }
2793
2794 ret = AP_TX_BUFFERED;
2795
2796 out:
2797 if (sta != NULL) {
2798 if (ret == AP_TX_CONTINUE ||
2799 ret == AP_TX_CONTINUE_NOT_AUTHORIZED) {
2800 sta->tx_packets++;
2801 sta->tx_bytes += skb->len;
2802 sta->last_tx = jiffies;
2803 }
2804
2805 if ((ret == AP_TX_CONTINUE ||
2806 ret == AP_TX_CONTINUE_NOT_AUTHORIZED) &&
2807 sta->crypt && tx->host_encrypt) {
2808 tx->crypt = sta->crypt;
2809 tx->sta_ptr = sta; /* hostap_handle_sta_release() will
2810 * be called to release sta info
2811 * later */
2812 } else
2813 atomic_dec(&sta->users);
2814 }
2815
2816 return ret;
2817}
2818
2819
2820void hostap_handle_sta_release(void *ptr)
2821{
2822 struct sta_info *sta = ptr;
2823 atomic_dec(&sta->users);
2824}
2825
2826
2827/* Called only as a tasklet (software IRQ) */
2828void hostap_handle_sta_tx_exc(local_info_t *local, struct sk_buff *skb)
2829{
2830 struct sta_info *sta;
James Ketrenosd0416742005-09-21 12:23:49 -05002831 struct ieee80211_hdr_4addr *hdr;
Jouni Malinenff1d2762005-05-12 22:54:16 -04002832 struct hostap_skb_tx_data *meta;
2833
James Ketrenosd0416742005-09-21 12:23:49 -05002834 hdr = (struct ieee80211_hdr_4addr *) skb->data;
Jouni Malinenff1d2762005-05-12 22:54:16 -04002835 meta = (struct hostap_skb_tx_data *) skb->cb;
2836
2837 spin_lock(&local->ap->sta_table_lock);
2838 sta = ap_get_sta(local->ap, hdr->addr1);
2839 if (!sta) {
2840 spin_unlock(&local->ap->sta_table_lock);
Johannes Berge1749612008-10-27 15:59:26 -07002841 PDEBUG(DEBUG_AP, "%s: Could not find STA %pM"
Joe Perches0795af52007-10-03 17:59:30 -07002842 " for this TX error (@%lu)\n",
Johannes Berge1749612008-10-27 15:59:26 -07002843 local->dev->name, hdr->addr1, jiffies);
Jouni Malinenff1d2762005-05-12 22:54:16 -04002844 return;
2845 }
2846
2847 sta->tx_since_last_failure = 0;
2848 sta->tx_consecutive_exc++;
Jeff Garzik74fae822005-07-31 13:08:32 -04002849
Jouni Malinenff1d2762005-05-12 22:54:16 -04002850 if (sta->tx_consecutive_exc >= WLAN_RATE_DECREASE_THRESHOLD &&
2851 sta->tx_rate_idx > 0 && meta->rate <= sta->tx_rate) {
2852 /* use next lower rate */
2853 int old, rate;
2854 old = rate = sta->tx_rate_idx;
2855 while (rate > 0) {
2856 rate--;
2857 if (ap_tx_rate_ok(rate, sta, local)) {
2858 sta->tx_rate_idx = rate;
2859 break;
2860 }
2861 }
2862 if (old != sta->tx_rate_idx) {
2863 switch (sta->tx_rate_idx) {
2864 case 0: sta->tx_rate = 10; break;
2865 case 1: sta->tx_rate = 20; break;
2866 case 2: sta->tx_rate = 55; break;
2867 case 3: sta->tx_rate = 110; break;
2868 default: sta->tx_rate = 0; break;
2869 }
Johannes Berge1749612008-10-27 15:59:26 -07002870 PDEBUG(DEBUG_AP,
2871 "%s: STA %pM TX rate lowered to %d\n",
2872 local->dev->name, sta->addr, sta->tx_rate);
Jouni Malinenff1d2762005-05-12 22:54:16 -04002873 }
2874 sta->tx_consecutive_exc = 0;
2875 }
2876 spin_unlock(&local->ap->sta_table_lock);
2877}
2878
2879
2880static void hostap_update_sta_ps2(local_info_t *local, struct sta_info *sta,
2881 int pwrmgt, int type, int stype)
2882{
2883 if (pwrmgt && !(sta->flags & WLAN_STA_PS)) {
2884 sta->flags |= WLAN_STA_PS;
Johannes Berge1749612008-10-27 15:59:26 -07002885 PDEBUG(DEBUG_PS2, "STA %pM changed to use PS "
Jouni Malinenff1d2762005-05-12 22:54:16 -04002886 "mode (type=0x%02X, stype=0x%02X)\n",
Johannes Berge1749612008-10-27 15:59:26 -07002887 sta->addr, type >> 2, stype >> 4);
Jouni Malinenff1d2762005-05-12 22:54:16 -04002888 } else if (!pwrmgt && (sta->flags & WLAN_STA_PS)) {
2889 sta->flags &= ~WLAN_STA_PS;
Johannes Berge1749612008-10-27 15:59:26 -07002890 PDEBUG(DEBUG_PS2, "STA %pM changed to not use "
Jouni Malinenff1d2762005-05-12 22:54:16 -04002891 "PS mode (type=0x%02X, stype=0x%02X)\n",
Johannes Berge1749612008-10-27 15:59:26 -07002892 sta->addr, type >> 2, stype >> 4);
Jouni Malinen4339d322005-08-14 19:08:44 -07002893 if (type != IEEE80211_FTYPE_CTL ||
2894 stype != IEEE80211_STYPE_PSPOLL)
Jouni Malinenff1d2762005-05-12 22:54:16 -04002895 schedule_packet_send(local, sta);
2896 }
2897}
2898
2899
2900/* Called only as a tasklet (software IRQ). Called for each RX frame to update
Jouni Malinenc0f72ca2005-08-14 19:08:43 -07002901 * STA power saving state. pwrmgt is a flag from 802.11 frame_ctl field. */
James Ketrenosd0416742005-09-21 12:23:49 -05002902int hostap_update_sta_ps(local_info_t *local, struct ieee80211_hdr_4addr *hdr)
Jouni Malinenff1d2762005-05-12 22:54:16 -04002903{
2904 struct sta_info *sta;
2905 u16 fc;
2906
2907 spin_lock(&local->ap->sta_table_lock);
2908 sta = ap_get_sta(local->ap, hdr->addr2);
2909 if (sta)
2910 atomic_inc(&sta->users);
2911 spin_unlock(&local->ap->sta_table_lock);
2912
2913 if (!sta)
2914 return -1;
2915
Jouni Malinenc0f72ca2005-08-14 19:08:43 -07002916 fc = le16_to_cpu(hdr->frame_ctl);
Jouni Malinenb2f4a2e2005-08-14 21:00:01 -07002917 hostap_update_sta_ps2(local, sta, fc & IEEE80211_FCTL_PM,
Jouni Malinen4339d322005-08-14 19:08:44 -07002918 WLAN_FC_GET_TYPE(fc), WLAN_FC_GET_STYPE(fc));
Jouni Malinenff1d2762005-05-12 22:54:16 -04002919
2920 atomic_dec(&sta->users);
2921 return 0;
2922}
2923
2924
2925/* Called only as a tasklet (software IRQ). Called for each RX frame after
2926 * getting RX header and payload from hardware. */
2927ap_rx_ret hostap_handle_sta_rx(local_info_t *local, struct net_device *dev,
2928 struct sk_buff *skb,
2929 struct hostap_80211_rx_status *rx_stats,
2930 int wds)
2931{
2932 int ret;
2933 struct sta_info *sta;
2934 u16 fc, type, stype;
James Ketrenosd0416742005-09-21 12:23:49 -05002935 struct ieee80211_hdr_4addr *hdr;
Jouni Malinenff1d2762005-05-12 22:54:16 -04002936
2937 if (local->ap == NULL)
2938 return AP_RX_CONTINUE;
2939
James Ketrenosd0416742005-09-21 12:23:49 -05002940 hdr = (struct ieee80211_hdr_4addr *) skb->data;
Jouni Malinenff1d2762005-05-12 22:54:16 -04002941
Jouni Malinenc0f72ca2005-08-14 19:08:43 -07002942 fc = le16_to_cpu(hdr->frame_ctl);
Jouni Malinen4339d322005-08-14 19:08:44 -07002943 type = WLAN_FC_GET_TYPE(fc);
2944 stype = WLAN_FC_GET_STYPE(fc);
Jouni Malinenff1d2762005-05-12 22:54:16 -04002945
2946 spin_lock(&local->ap->sta_table_lock);
2947 sta = ap_get_sta(local->ap, hdr->addr2);
2948 if (sta)
2949 atomic_inc(&sta->users);
2950 spin_unlock(&local->ap->sta_table_lock);
2951
2952 if (sta && !(sta->flags & WLAN_STA_AUTHORIZED))
2953 ret = AP_RX_CONTINUE_NOT_AUTHORIZED;
2954 else
2955 ret = AP_RX_CONTINUE;
2956
2957
Jouni Malinenb2f4a2e2005-08-14 21:00:01 -07002958 if (fc & IEEE80211_FCTL_TODS) {
Jouni Malinenff1d2762005-05-12 22:54:16 -04002959 if (!wds && (sta == NULL || !(sta->flags & WLAN_STA_ASSOC))) {
2960 if (local->hostapd) {
2961 prism2_rx_80211(local->apdev, skb, rx_stats,
2962 PRISM2_RX_NON_ASSOC);
2963#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
2964 } else {
2965 printk(KERN_DEBUG "%s: dropped received packet"
Johannes Berge1749612008-10-27 15:59:26 -07002966 " from non-associated STA %pM"
Jouni Malinenff1d2762005-05-12 22:54:16 -04002967 " (type=0x%02x, subtype=0x%02x)\n",
Johannes Berge1749612008-10-27 15:59:26 -07002968 dev->name, hdr->addr2,
Jouni Malinen4339d322005-08-14 19:08:44 -07002969 type >> 2, stype >> 4);
Jouni Malinenff1d2762005-05-12 22:54:16 -04002970 hostap_rx(dev, skb, rx_stats);
2971#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
2972 }
2973 ret = AP_RX_EXIT;
2974 goto out;
2975 }
Jouni Malinenb2f4a2e2005-08-14 21:00:01 -07002976 } else if (fc & IEEE80211_FCTL_FROMDS) {
Jouni Malinenff1d2762005-05-12 22:54:16 -04002977 if (!wds) {
2978 /* FromDS frame - not for us; probably
2979 * broadcast/multicast in another BSS - drop */
2980 if (memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN) == 0) {
2981 printk(KERN_DEBUG "Odd.. FromDS packet "
2982 "received with own BSSID\n");
2983 hostap_dump_rx_80211(dev->name, skb, rx_stats);
2984 }
2985 ret = AP_RX_DROP;
2986 goto out;
2987 }
Jouni Malinen4339d322005-08-14 19:08:44 -07002988 } else if (stype == IEEE80211_STYPE_NULLFUNC && sta == NULL &&
Jouni Malinenff1d2762005-05-12 22:54:16 -04002989 memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN) == 0) {
2990
2991 if (local->hostapd) {
2992 prism2_rx_80211(local->apdev, skb, rx_stats,
2993 PRISM2_RX_NON_ASSOC);
2994#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
2995 } else {
2996 /* At least Lucent f/w seems to send data::nullfunc
2997 * frames with no ToDS flag when the current AP returns
2998 * after being unavailable for some time. Speed up
2999 * re-association by informing the station about it not
3000 * being associated. */
Johannes Berge1749612008-10-27 15:59:26 -07003001 printk(KERN_DEBUG "%s: rejected received nullfunc frame"
3002 " without ToDS from not associated STA %pM\n",
3003 dev->name, hdr->addr2);
Jouni Malinenff1d2762005-05-12 22:54:16 -04003004 hostap_rx(dev, skb, rx_stats);
3005#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
3006 }
3007 ret = AP_RX_EXIT;
3008 goto out;
Jouni Malinen4339d322005-08-14 19:08:44 -07003009 } else if (stype == IEEE80211_STYPE_NULLFUNC) {
Jouni Malinenff1d2762005-05-12 22:54:16 -04003010 /* At least Lucent cards seem to send periodic nullfunc
3011 * frames with ToDS. Let these through to update SQ
3012 * stats and PS state. Nullfunc frames do not contain
3013 * any data and they will be dropped below. */
3014 } else {
3015 /* If BSSID (Addr3) is foreign, this frame is a normal
3016 * broadcast frame from an IBSS network. Drop it silently.
3017 * If BSSID is own, report the dropping of this frame. */
3018 if (memcmp(hdr->addr3, dev->dev_addr, ETH_ALEN) == 0) {
Johannes Berge1749612008-10-27 15:59:26 -07003019 printk(KERN_DEBUG "%s: dropped received packet from %pM"
3020 " with no ToDS flag "
Joe Perches0795af52007-10-03 17:59:30 -07003021 "(type=0x%02x, subtype=0x%02x)\n", dev->name,
Johannes Berge1749612008-10-27 15:59:26 -07003022 hdr->addr2, type >> 2, stype >> 4);
Jouni Malinenff1d2762005-05-12 22:54:16 -04003023 hostap_dump_rx_80211(dev->name, skb, rx_stats);
3024 }
3025 ret = AP_RX_DROP;
3026 goto out;
3027 }
3028
3029 if (sta) {
Jouni Malinenb2f4a2e2005-08-14 21:00:01 -07003030 hostap_update_sta_ps2(local, sta, fc & IEEE80211_FCTL_PM,
Jouni Malinenff1d2762005-05-12 22:54:16 -04003031 type, stype);
3032
3033 sta->rx_packets++;
3034 sta->rx_bytes += skb->len;
3035 sta->last_rx = jiffies;
3036 }
3037
Jouni Malinen4339d322005-08-14 19:08:44 -07003038 if (local->ap->nullfunc_ack && stype == IEEE80211_STYPE_NULLFUNC &&
Jouni Malinenb2f4a2e2005-08-14 21:00:01 -07003039 fc & IEEE80211_FCTL_TODS) {
Jouni Malinenff1d2762005-05-12 22:54:16 -04003040 if (local->hostapd) {
3041 prism2_rx_80211(local->apdev, skb, rx_stats,
3042 PRISM2_RX_NULLFUNC_ACK);
3043#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
3044 } else {
3045 /* some STA f/w's seem to require control::ACK frame
3046 * for data::nullfunc, but Prism2 f/w 0.8.0 (at least
3047 * from Compaq) does not send this.. Try to generate
3048 * ACK for these frames from the host driver to make
3049 * power saving work with, e.g., Lucent WaveLAN f/w */
3050 hostap_rx(dev, skb, rx_stats);
3051#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
3052 }
3053 ret = AP_RX_EXIT;
3054 goto out;
3055 }
3056
3057 out:
3058 if (sta)
3059 atomic_dec(&sta->users);
3060
3061 return ret;
3062}
3063
3064
3065/* Called only as a tasklet (software IRQ) */
3066int hostap_handle_sta_crypto(local_info_t *local,
James Ketrenosd0416742005-09-21 12:23:49 -05003067 struct ieee80211_hdr_4addr *hdr,
John W. Linville274bfb82008-10-29 11:35:05 -04003068 struct lib80211_crypt_data **crypt,
Jouni Malinen62fe7e32005-07-30 20:43:20 -07003069 void **sta_ptr)
Jouni Malinenff1d2762005-05-12 22:54:16 -04003070{
3071 struct sta_info *sta;
3072
3073 spin_lock(&local->ap->sta_table_lock);
3074 sta = ap_get_sta(local->ap, hdr->addr2);
3075 if (sta)
3076 atomic_inc(&sta->users);
3077 spin_unlock(&local->ap->sta_table_lock);
3078
3079 if (!sta)
3080 return -1;
3081
3082 if (sta->crypt) {
3083 *crypt = sta->crypt;
3084 *sta_ptr = sta;
3085 /* hostap_handle_sta_release() will be called to release STA
3086 * info */
3087 } else
3088 atomic_dec(&sta->users);
3089
3090 return 0;
3091}
3092
3093
3094/* Called only as a tasklet (software IRQ) */
3095int hostap_is_sta_assoc(struct ap_data *ap, u8 *sta_addr)
3096{
3097 struct sta_info *sta;
3098 int ret = 0;
3099
3100 spin_lock(&ap->sta_table_lock);
3101 sta = ap_get_sta(ap, sta_addr);
3102 if (sta != NULL && (sta->flags & WLAN_STA_ASSOC) && !sta->ap)
3103 ret = 1;
3104 spin_unlock(&ap->sta_table_lock);
3105
3106 return ret;
3107}
3108
3109
3110/* Called only as a tasklet (software IRQ) */
3111int hostap_is_sta_authorized(struct ap_data *ap, u8 *sta_addr)
3112{
3113 struct sta_info *sta;
3114 int ret = 0;
3115
3116 spin_lock(&ap->sta_table_lock);
3117 sta = ap_get_sta(ap, sta_addr);
3118 if (sta != NULL && (sta->flags & WLAN_STA_ASSOC) && !sta->ap &&
3119 ((sta->flags & WLAN_STA_AUTHORIZED) ||
3120 ap->local->ieee_802_1x == 0))
3121 ret = 1;
3122 spin_unlock(&ap->sta_table_lock);
3123
3124 return ret;
3125}
3126
3127
3128/* Called only as a tasklet (software IRQ) */
3129int hostap_add_sta(struct ap_data *ap, u8 *sta_addr)
3130{
3131 struct sta_info *sta;
3132 int ret = 1;
3133
3134 if (!ap)
3135 return -1;
3136
3137 spin_lock(&ap->sta_table_lock);
3138 sta = ap_get_sta(ap, sta_addr);
3139 if (sta)
3140 ret = 0;
3141 spin_unlock(&ap->sta_table_lock);
3142
3143 if (ret == 1) {
3144 sta = ap_add_sta(ap, sta_addr);
3145 if (!sta)
Adrian Bunk54b85f42006-03-19 19:21:45 -08003146 return -1;
Jouni Malinenff1d2762005-05-12 22:54:16 -04003147 sta->flags = WLAN_STA_AUTH | WLAN_STA_ASSOC;
3148 sta->ap = 1;
3149 memset(sta->supported_rates, 0, sizeof(sta->supported_rates));
3150 /* No way of knowing which rates are supported since we did not
3151 * get supported rates element from beacon/assoc req. Assume
3152 * that remote end supports all 802.11b rates. */
3153 sta->supported_rates[0] = 0x82;
3154 sta->supported_rates[1] = 0x84;
3155 sta->supported_rates[2] = 0x0b;
3156 sta->supported_rates[3] = 0x16;
3157 sta->tx_supp_rates = WLAN_RATE_1M | WLAN_RATE_2M |
3158 WLAN_RATE_5M5 | WLAN_RATE_11M;
3159 sta->tx_rate = 110;
3160 sta->tx_max_rate = sta->tx_rate_idx = 3;
3161 }
3162
3163 return ret;
3164}
3165
3166
3167/* Called only as a tasklet (software IRQ) */
3168int hostap_update_rx_stats(struct ap_data *ap,
James Ketrenosd0416742005-09-21 12:23:49 -05003169 struct ieee80211_hdr_4addr *hdr,
Jouni Malinenff1d2762005-05-12 22:54:16 -04003170 struct hostap_80211_rx_status *rx_stats)
3171{
3172 struct sta_info *sta;
3173
3174 if (!ap)
3175 return -1;
3176
3177 spin_lock(&ap->sta_table_lock);
3178 sta = ap_get_sta(ap, hdr->addr2);
3179 if (sta) {
3180 sta->last_rx_silence = rx_stats->noise;
3181 sta->last_rx_signal = rx_stats->signal;
3182 sta->last_rx_rate = rx_stats->rate;
Jean Tourrilhesc28df16e2005-09-23 21:58:59 -07003183 sta->last_rx_updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
Jouni Malinenff1d2762005-05-12 22:54:16 -04003184 if (rx_stats->rate == 10)
3185 sta->rx_count[0]++;
3186 else if (rx_stats->rate == 20)
3187 sta->rx_count[1]++;
3188 else if (rx_stats->rate == 55)
3189 sta->rx_count[2]++;
3190 else if (rx_stats->rate == 110)
3191 sta->rx_count[3]++;
3192 }
3193 spin_unlock(&ap->sta_table_lock);
3194
3195 return sta ? 0 : -1;
3196}
3197
3198
3199void hostap_update_rates(local_info_t *local)
3200{
Matthias Kaehlckec1505732007-05-28 09:38:48 -07003201 struct sta_info *sta;
Jouni Malinenff1d2762005-05-12 22:54:16 -04003202 struct ap_data *ap = local->ap;
3203
3204 if (!ap)
3205 return;
3206
3207 spin_lock_bh(&ap->sta_table_lock);
Matthias Kaehlckec1505732007-05-28 09:38:48 -07003208 list_for_each_entry(sta, &ap->sta_list, list) {
Jouni Malinenff1d2762005-05-12 22:54:16 -04003209 prism2_check_tx_rates(sta);
3210 }
3211 spin_unlock_bh(&ap->sta_table_lock);
3212}
3213
3214
Adrian Bunk5fad5a22006-01-14 03:09:34 +01003215void * ap_crypt_get_ptrs(struct ap_data *ap, u8 *addr, int permanent,
John W. Linville274bfb82008-10-29 11:35:05 -04003216 struct lib80211_crypt_data ***crypt)
Jouni Malinenff1d2762005-05-12 22:54:16 -04003217{
3218 struct sta_info *sta;
3219
3220 spin_lock_bh(&ap->sta_table_lock);
3221 sta = ap_get_sta(ap, addr);
3222 if (sta)
3223 atomic_inc(&sta->users);
3224 spin_unlock_bh(&ap->sta_table_lock);
3225
3226 if (!sta && permanent)
3227 sta = ap_add_sta(ap, addr);
3228
3229 if (!sta)
3230 return NULL;
3231
3232 if (permanent)
3233 sta->flags |= WLAN_STA_PERM;
3234
3235 *crypt = &sta->crypt;
3236
3237 return sta;
3238}
3239
3240
3241void hostap_add_wds_links(local_info_t *local)
3242{
3243 struct ap_data *ap = local->ap;
Matthias Kaehlckec1505732007-05-28 09:38:48 -07003244 struct sta_info *sta;
Jouni Malinenff1d2762005-05-12 22:54:16 -04003245
3246 spin_lock_bh(&ap->sta_table_lock);
Matthias Kaehlckec1505732007-05-28 09:38:48 -07003247 list_for_each_entry(sta, &ap->sta_list, list) {
Jouni Malinenff1d2762005-05-12 22:54:16 -04003248 if (sta->ap)
3249 hostap_wds_link_oper(local, sta->addr, WDS_ADD);
3250 }
3251 spin_unlock_bh(&ap->sta_table_lock);
3252
3253 schedule_work(&local->ap->wds_oper_queue);
3254}
3255
3256
3257void hostap_wds_link_oper(local_info_t *local, u8 *addr, wds_oper_type type)
3258{
3259 struct wds_oper_data *entry;
3260
3261 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
3262 if (!entry)
3263 return;
3264 memcpy(entry->addr, addr, ETH_ALEN);
3265 entry->type = type;
3266 spin_lock_bh(&local->lock);
3267 entry->next = local->ap->wds_oper_entries;
3268 local->ap->wds_oper_entries = entry;
3269 spin_unlock_bh(&local->lock);
3270
3271 schedule_work(&local->ap->wds_oper_queue);
3272}
3273
3274
3275EXPORT_SYMBOL(hostap_init_data);
3276EXPORT_SYMBOL(hostap_init_ap_proc);
3277EXPORT_SYMBOL(hostap_free_data);
3278EXPORT_SYMBOL(hostap_check_sta_fw_version);
Jouni Malinenff1d2762005-05-12 22:54:16 -04003279EXPORT_SYMBOL(hostap_handle_sta_tx_exc);
Jouni Malinenff1d2762005-05-12 22:54:16 -04003280#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
Jouni Malinenff1d2762005-05-12 22:54:16 -04003281#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */